1Test::MockModule(3)   User Contributed Perl Documentation  Test::MockModule(3)
2
3
4

NAME

6       Test::MockModule - Override subroutines in a module for unit testing
7

SYNOPSIS

9          use Module::Name;
10          use Test::MockModule;
11
12          {
13              my $module = new Test::MockModule('Module::Name');
14              $module->mock('subroutine', sub { ... });
15              Module::Name::subroutine(@args); # mocked
16          }
17
18          Module::Name::subroutine(@args); # original subroutine
19

DESCRIPTION

21       "Test::MockModule" lets you temporarily redefine subroutines in other
22       packages for the purposes of unit testing.
23
24       A "Test::MockModule" object is set up to mock subroutines for a given
25       module. The object remembers the original subroutine so it can be
26       easily restored. This happens automatically when all MockModule objects
27       for the given module go out of scope, or when you "unmock()" the
28       subroutine.
29

METHODS

31       new($package[, %options])
32           Returns an object that will mock subroutines in the specified
33           $package.
34
35           If there is no $VERSION defined in $package, the module will be
36           automatically loaded. You can override this behaviour by setting
37           the "no_auto" option:
38
39               my $mock = new Test::MockModule('Module::Name', no_auto => 1);
40
41       get_package()
42           Returns the target package name for the mocked subroutines
43
44       is_mocked($subroutine)
45           Returns a boolean value indicating whether or not the subroutine is
46           currently mocked
47
48       mock($subroutine => \&coderef)
49           Temporarily replaces one or more subroutines in the mocked module.
50           A subroutine can be mocked with a code reference or a scalar. A
51           scalar will be recast as a subroutine that returns the scalar.
52
53           The following statements are equivalent:
54
55               $module->mock(purge => 'purged');
56               $module->mock(purge => sub { return 'purged'});
57
58               $module->mock(updated => [localtime()]);
59               $module->mock(updated => sub { return [localtime()]});
60
61           However, "undef" is a special case. If you mock a subroutine with
62           "undef" it will install an empty subroutine
63
64               $module->mock(purge => undef);
65               $module->mock(purge => sub { });
66
67           rather than a subroutine that returns "undef":
68
69               $module->mock(purge => sub { undef });
70
71           You can call "mock()" for the same subroutine many times, but when
72           you call "unmock()", the original subroutine is restored (not the
73           last mocked instance).
74
75       original($subroutine)
76           Returns the original (unmocked) subroutine
77
78       unmock($subroutine [, ...])
79           Restores the original $subroutine. You can specify a list of
80           subroutines to "unmock()" in one go.
81
82       unmock_all()
83           Restores all the subroutines in the package that were mocked. This
84           is automatically called when all "Test::MockObject" objects for the
85           given package go out of scope.
86

SEE ALSO

88       Test::MockObject::Extends
89
90       Sub::Override
91

AUTHOR

93       Simon Flack <simonflk _AT_ cpan.org>
94
96       Copyright 2004 Simon Flack <simonflk _AT_ cpan.org>.  All rights
97       reserved
98
99       You may distribute under the terms of either the GNU General Public
100       License or the Artistic License, as specified in the Perl README file.
101
102
103
104perl v5.12.0                      2005-03-24               Test::MockModule(3)
Impressum