Test::MockModule - Override subroutines in a module for unit testing |
Test::MockModule - Override subroutines in a module for unit testing
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
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.
$package
.
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 = new Test::MockModule('Module::Name', no_auto => 1);
get_package()
is_mocked($subroutine)
The following statements are equivalent:
$module->mock(purge => 'purged'); $module->mock(purge => sub { return 'purged'});
$module->mock(updated => [localtime()]); $module->mock(updated => sub { return [localtime()]});
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).
original($subroutine)
$subroutine
. You can specify a list of subroutines to
unmock()
in one go.
unmock_all()
Test::MockObject
objects for the given package
go out of scope.
the Test::MockObject::Extends manpage
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.
Test::MockModule - Override subroutines in a module for unit testing |