NAME
Test::MockModule - Override subroutines in a module for unit testing
SYNOPSIS
use Module::Name;
use Test::MockModule;
{
my $module = new Test::MockModule('Module::Name');
$module->mock('subroutine', sub { ... });
Module::Name::subroutine(@args); # mocked
}
Module::Name::subroutine(@args); # original subroutine
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 explicitly.
METHODS
- new($package[, %options])
-
Returns an object that will mock subroutines in the specified
$package.If there is no
$VERSIONdefined in$package, the module will be automatically loaded. You can override this behaviour by setting theno_autooption:my $mock = new Test::MockModule('Module::Name', no_auto => 1); - get_package()
-
Returns the target package name for the mocked subroutines
- is_mocked($subroutine)
-
Returns a boolean value indicating whether or not the subroutine is currently mocked
- mock($subroutine[, \&coderef])
-
Temporarily replaces
$subroutinewith the supplied&coderef. The code reference is optional, and defaults to an empty subroutine if omitted.You can call
mock()for the same subroutine many times, but when you callunmock(), the original subroutine is restored (not the last mocked instance). - original($subroutine)
-
Returns the original (unmocked) subroutine
- unmock($subroutine)
-
Restores the original
$subroutine - unmock_all()
-
Restores all the subroutines in the package that were mocked. This is automatically called when all
Test::MockObjectobjects for the given package go out of scope.
SEE ALSO
AUTHOR
Simon Flack <simonflk _AT_ cpan.org>
COPYRIGHT
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.