![]() |
![]()
| ![]() |
![]()
NAMETest::MockModule - Override subroutines in a module for unit testing SYNOPSISuse 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 { ... }); # This will die() if another_subroutine() is defined. $module->define('another_subroutine', sub { ... }); } { # you can also chain new/mock/redefine/define Test::MockModule->new('Module::Name') ->mock( one_subroutine => sub { ... }) ->redefine( other_subroutine => sub { ... } ) ->define( a_new_sub => 1234 ); } 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" } # If you want to prevent noop and mock from working, you can # load Test::MockModule in strict mode. use Test::MockModule qw/strict/; my $module = Test::MockModule->new('Module::Name'); # Redefined the other_subroutine or dies if it's not there. $module->redefine('other_subroutine', sub { ... }); # Dies since you specified you wanted strict mode. $module->mock('subroutine', sub { ... }); # Turn strictness off in this lexical scope { use Test::MockModule 'nostrict'; # ->mock() works now $module->mock('subroutine', sub { ... }); } # Back in the strict scope, so mock() dies here $module->mock('subroutine', sub { ... }); DESCRIPTION"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. STRICT MODEOne of the weaknesses of testing using mocks is that the implementation of the interface that you are mocking might change, while your mocks get left alone. You are not now mocking what you thought you were, and your mocks might now be hiding bugs that will only be spotted in production. To help prevent this you can load Test::MockModule in 'strict' mode: use Test::MockModule qw(strict); This will disable use of the mock() method, making it a fatal runtime error. You should instead define mocks using redefine(), which will only mock things that already exist and die if you try to redefine something that doesn't exist. Strictness is lexically scoped, so you can do this in one file: use Test::MockModule qw(strict); ...->redefine(...); and this in another: use Test::MockModule; # the default is nostrict ...->mock(...); You can even mix n match at different places in a single file thus: use Test::MockModule qw(strict); # here mock() dies { use Test::MockModule qw(nostrict); # here mock() works } # here mock() goes back to dieing use Test::MockModule qw(nostrict); # and from here on mock() works again NB that strictness must be defined at compile-time, and set using "use". If you think you're going to try and be clever by calling Test::MockModule's import() method at runtime then what happens in undefined, with results differing from one version of perl to another. What larks! METHODS
SEE ALSOTest::MockObject::Extends Sub::Override AUTHORSCurrent Maintainer: Geoff Franks <gfranks@cpan.org> Original Author: Simon Flack <simonflk _AT_ cpan.org> Lexical scoping of strictness: David Cantrell <david@cantrell.org.uk> COPYRIGHTCopyright 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.
|