Test::AutoMock - A mock that can be used with a minimum setup
use Test::AutoMock qw(mock manager); use Test::More import => [qw(is note done_testing)]; # a black box function you want to test sub get_metacpan { my $ua = shift; my $response = $ua->get('https://metacpan.org/'); if ($response->is_success) { return $response->decoded_content; # or whatever } else { die $response->status_line; } } # build and set up the mock my $mock_ua = mock( methods => { # implement only the method you are interested in 'get->decoded_content' => "Hello, metacpan!\n", }, ); # action first my $body = get_metacpan($mock_ua); # then, assertion is $body, "Hello, metacpan!\n"; manager($mock_ua)->called_with_ok('get->is_success' => []); manager($mock_ua)->not_called_ok('get->status_line'); # print all recorded calls for (manager($mock_ua)->calls) { my ($method, $args) = @$_; note "$method(" . join(', ', @$args) . ")"; }
Test::AutoMock is a mock module designed to be used with a minimal setup. AutoMock can respond to any method call and returns a new AutoMock instance as a return value. Therefore, you can use it as a mock object without having to define all the methods. Even if method calls are nested, there is no problem.
AutoMock records all method calls on all descendants. You can verify the method calls and its arguments after using the mock. This is not the "record and replay" model but the "action and assertion" model.
You can also mock many overloaded operators and hashes, arrays with Test::AutoMock::Mock::Overloaded. If you want to apply monkey patch to use AutoMock, check Test::AutoMock::Patch.
Test::AutoMock is inspired by Python3's unittest.mock module.
This module is under development. The API, including names of classes and methods, may be subject to BACKWARD INCOMPATIBLE CHANGES.
mock
my $mock = mock( methods => { agent => 'libwww-perl/AutoMock', 'get->is_success' => sub { 1 }, }, isa => 'LWP::UserAgent', );
Create Test::AutoMock::Mock::Basic instance. It takes the following parameters.
methods
A hash-ref of method definitions. See Test::AutoMock::Manager::add_method.
isa
A super class of this mock. See Test::AutoMock::Manager::isa. To specify multiple classes, use array-ref.
mock_overloaded
It is the same as the mock method except that the generated instance is Test::AutoMock::Mock::Overloaded.
manager
Access the Test::AutoMock::Manager of the mock instance. You can set up and verify the mock with the Manager object. See Test::AutoMock::Manager for details.
All Test::AutoMock::Mock::Basic and Test::AutoMock::Mock::Overloaded instances have the Manager class. The manager and the mock correspond one to one. In fact, manager($mock)->mock == $mock and manager($manager->mock) == $manager hold.
manager($mock)->mock == $mock
manager($manager->mock) == $manager
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.