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

Mocked web test

  • Here we need to mock the get function as it is already in the MyWebAPI module
  • If we mocked the one inside LWP::Simple that would not impact the one we already have in the MyWebAPI module.
use strict;
use warnings;

use Test::More;

use Test::Mock::Simple;
use MyWebAPI;

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

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

$mock->add(get => sub {
    return 'Beyonce Beyonce Miley Cyrus';
});
is_deeply $w->count_strings('Beyonce', 'Miley Cyrus'),
    {
        'Beyonce'     => 2,
        'Miley Cyrus' => 1,
    };

$mock->add(get => sub {
    return 'Beyonce';
});
is_deeply $w->count_strings('Beyonce', 'Miley Cyrus'),
    {
        Beyonce => 1,
        'Miley Cyrus' => 0,
    };

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


done_testing;