Test::MockModule(3pm) | User Contributed Perl Documentation | Test::MockModule(3pm) |
Test::MockModule - Override subroutines in a module for unit testing
use Module::Name; use Test::MockModule; { my $module = Test::MockModule->new('Module::Name'); $module->mock('subroutine', sub { ... }); Module::Name::subroutine(@args); # mocked #Same effect, but this will die() if other_subroutine() #doesn't already exist, which is often desirable. $module->redefine('other_subroutine', sub { ... }); } Module::Name::subroutine(@args); # original subroutine # Working with objects use Foo; use Test::MockModule; { my $mock = Test::MockModule->new('Foo'); $mock->mock(foo => sub { print "Foo!\n"; }); my $foo = Foo->new(); $foo->foo(); # prints "Foo!\n" }
"Test::MockModule" lets you temporarily redefine subroutines in other packages for the purposes of unit testing.
A "Test::MockModule" object is set up to mock subroutines for a given module. The object remembers the original subroutine so it can be easily restored. This happens automatically when all MockModule objects for the given module go out of scope, or when you "unmock()" the subroutine.
If there is no $VERSION defined in $package, the module will be automatically loaded. You can override this behaviour by setting the "no_auto" option:
my $mock = Test::MockModule->new('Module::Name', no_auto => 1);
The following statements are equivalent:
$module->mock(purge => 'purged'); $module->mock(purge => sub { return 'purged'});
When dealing with references, things behave slightly differently. The following statements are NOT equivalent:
# Returns the same arrayref each time, with the localtime() at time of mocking $module->mock(updated => [localtime()]); # Returns a new arrayref each time, with up-to-date localtime() value $module->mock(updated => sub { return [localtime()]});
The following statements are in fact equivalent:
my $array_ref = [localtime()] $module->mock(updated => $array_ref) $module->mock(updated => sub { return $array_ref });
However, "undef" is a special case. If you mock a subroutine with "undef" it will install an empty subroutine
$module->mock(purge => undef); $module->mock(purge => sub { });
rather than a subroutine that returns "undef":
$module->mock(purge => sub { undef });
You can call "mock()" for the same subroutine many times, but when you call "unmock()", the original subroutine is restored (not the last mocked instance).
MOCKING + EXPORT
If you are trying to mock a subroutine exported from another module, this may not behave as you initially would expect, since Test::MockModule is only mocking at the target module, not anything importing that module. If you mock the local package, or use a fully qualified function name, you will get the behavior you desire:
use Test::MockModule; use Test::More; use POSIX qw/strftime/; my $posix = Test::MockModule->new("POSIX"); $posix->mock("strftime", "Yesterday"); is strftime("%D", localtime(time)), "Yesterday", "`strftime` was mocked successfully"; # Fails is POSIX::strftime("%D", localtime(time)), "Yesterday", "`strftime` was mocked successfully"; # Succeeds my $main = Test::MockModule->new("main", no_auto => 1); $main->mock("strftime", "today"); is strftime("%D", localtime(time)), "today", "`strftime` was mocked successfully"; # Succeeds
If you are trying to mock a subroutine that was exported into a module that you're trying to test, rather than mocking the subroutine in its originating module, you can instead mock it in the module you are testing:
package MyModule; use POSIX qw/strftime/; sub minus_twentyfour { return strftime("%a, %b %d, %Y", localtime(time - 86400)); } package main; use Test::More; use Test::MockModule; my $posix = Test::MockModule->new("POSIX"); $posix->mock("strftime", "Yesterday"); is MyModule::minus_twentyfour(), "Yesterday", "`minus-twentyfour` got mocked"; # fails my $mymodule = Test::MockModule->new("MyModule", no_auto => 1); $mymodule->mock("strftime", "Yesterday"); is MyModule::minus_twentyfour(), "Yesterday", "`minus-twentyfour` got mocked"; # succeeds
Note that redefine is also now checking if one of the parent provides the sub and will not die if it's available in the chain.
Here is a sample how to wrap a function with custom arguments using the original subroutine. This is useful when you cannot (do not) want to alter the original code to abstract one hardcoded argument pass to a function.
package MyModule; sub sample { return get_path_for("/a/b/c/d"); } sub get_path_for { ... # anything goes there... } package main; use Test::MockModule; my $mock = Test::MockModule->new("MyModule"); # replace all calls to get_path_for using a different argument $mock->redefine("get_path_for", sub { return $mock->original("get_path_for")->("/my/custom/path"); }); # or $mock->redefine("get_path_for", sub { my $path = shift; if ( $path && $path eq "/a/b/c/d" ) { # only alter calls with path set to "/a/b/c/d" return $mock->original("get_path_for")->("/my/custom/path"); } else { # preserve the original arguments return $mock->original("get_path_for")->(@_); } });
# Neuter a list of methods in one go $module->noop('purge', 'updated');
Test::MockObject::Extends
Sub::Override
Current Maintainer: Geoff Franks <gfranks@cpan.org>
Original Author: Simon Flack <simonflk _AT_ cpan.org>
Copyright 2004 Simon Flack <simonflk _AT_ cpan.org>. All rights reserved
You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
2018-09-07 | perl v5.26.2 |