Test::AutoMock::Patch - Monkey patch for returning AutoMock
use Test::AutoMock::Patch qw(patch_sub); # a black box function you want to test sub get_metacpan { my $ua = LWP::UserAgent->new; my $response = $ua->get('https://metacpan.org/'); if ($response->is_success) { return $response->decoded_content; # or whatever } else { die $response->status_line; } } # apply a monkey patch to LWP::UserAgent::new patch_sub { my $mock = shift; # set up the mock manager($mock)->add_method('get->decoded_content' => "Hello, metacpan!\n"); # call blackbox function my $body = get_metacpan(); # assertions is $body, "Hello, metacpan!\n"; manager($mock)->called_with_ok('get->is_success' => []); manager($mock)->not_called_ok('get->status_line'); } 'LWP::UserAgent::new';
Temporarily replace any subroutine and return AutoMock. It is convenient when mock can not be injected from outside.
patch_sub
patch_sub { my ($mock, $other_mock) = @_; # write your test using $mock } 'Path::To::subroutine', 'Path::To::other_subroutine';
Replace the specified subroutine with one that returns a mock, and execute the code in the block. The mock object is passed as the argument of the block by the number of replaced subroutines. After exiting the block, the patch is removed.
The generated mock object is an instance of Test::AutoMock::Mock::Overloaded.
It is a common usage to patch the class method used as a constructor.
Copyright (C) Masahiro Honma.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Masahiro Honma <hiratara@cpan.org>
To install Test::AutoMock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::AutoMock
CPAN shell
perl -MCPAN -e shell install Test::AutoMock
For more information on module installation, please visit the detailed CPAN module installation guide.