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.
examples/mock-lwp/t/webapi_mock.t
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;