Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mocking exception

  • What if the get function raises an exception, how will our code handle?
  • Hint: it does not, so this test will break
  • But this is how we could test what will happen in that case without trying to figure out how to reliably create one using the real LWP::Simple
use strict;
use warnings;

use Test::More;
use Test::Mock::Simple;
my $mock = Test::Mock::Simple->new(module => 'MyWebAPI');

my $w = MyWebAPI->new('http://www.dailymail.co.uk/');

$mock->add(get => sub {
    die 'Something went wrong';
});
is_deeply $w->count_strings('Beyonce', 'Miley Cyrus'),
    {
        Beyonce => 0,
        'Miley Cyrus' => 0,
    };


done_testing;