1Test::MockObject::ExtenUdsse(r3)Contributed Perl DocumenTteastti:o:nMockObject::Extends(3)
2
3
4

NAME

6       Test::MockObject::Extends - mock part of an object or class
7

SYNOPSIS

9         use Some::Class;
10         use Test::MockObject::Extends;
11
12         # create an object to mock
13         my $object      = Some::Class->new();
14
15         # wrap that same object with a mocking wrapper
16         $object         = Test::MockObject::Extends->new( $object );
17
18         # now chain mock and control calls
19         $object->set_true( 'parent_method' )
20                ->set_always( -grandparent_method => 1 )
21                ->clear();
22

DESCRIPTION

24       Test::MockObject::Extends lets you mock one or more methods of an
25       existing object or class.  This can be very handy when you're testing a
26       well-factored module that does almost exactly what you want.  Wouldn't
27       it be handy to take control of a method or two to make sure you receive
28       testable results?  Now you can.
29

METHODS

31       "new( $object ⎪ $class )"
32           "new()" takes one optional argument, the object or class to mock.
33           If you're mocking a method for an object that holds internal state,
34           create an appropriate object, then pass it to this constructor.
35           NOTE: this will modify the object in place.
36
37           If you're mocking an object that does not need state, as in the
38           cases where there's no internal data or you'll only be calling
39           class methods, or where you'll be mocking all of the access to
40           internal data, you can pass in the name of the class to mock par‐
41           tially.
42
43           If you've not yet loaded the class, this method will try to load it
44           for you.  This may fail, so beware.
45
46           If you pass no arguments, it will assume you really meant to create
47           a normal "Test::MockObject" object and will oblige you.
48
49           Note that if you pass a class, the object returned will appear to
50           be an instance of that class; this does not mock the class itself.
51
52       "mock( $methodname, $sub_ref )"
53           See the documentation for Test::MockObject for all of the ways to
54           mock methods and to retrieve method logging information.  These
55           methods return the invocant, so you can chain them.
56
57       "unmock( $methodname )"
58           Removes any active mocking of the named method.  This means any
59           calls to that method will hit the method of that name in the class
60           being mocked, if it exists.  This method returns the invocant, you
61           can chain it.
62
63       "isa( $class )"
64           As you'd expect from a mocked object, this will return true for the
65           class it's mocking.
66

INTERNAL METHODS

68       To do its magic, this module uses several internal methods:
69
70       * "check_class_loaded( $parent_class )"
71           This verifies that you have the mockee defined.  If not, it
72           attempts to load the corresponding module for you.
73
74       * "gen_autoload( $extended )"
75           Returns an AUTOLOAD subroutine for the mock object that checks that
76           the extended object (or class) can perform the requested method,
77           that Test::MockObject can perform it, or that the parent has an
78           appropriate AUTOLOAD of its own.  (It should have its own "can()"
79           in that case too though.)
80
81       * "gen_can( $extended )"
82           Returns a "can()" method for the mock object that respects the same
83           execution order as "gen_autoload()".
84
85       * "gen_isa( $extended )"
86           Returns an "isa()" method for the mock object that claims to be the
87           $extended object appropriately.
88
89       * "gen_get_parents( $extended )"
90           Returns a "__get_parents()" method for the mock object that claims
91           to be the $extended object appropriately.
92
93       * "gen_package( $extended )"
94           Creates a new unique package for the mock object with the appropri‐
95           ate methods already installed.
96
97       * "get_class( $invocant )"
98           Returns the class name of the invocant, whether it's an object or a
99           class name.
100

CAVEATS

102       There may be some weird corner cases with dynamically generated methods
103       in the mocked class.  You really should use subroutine declarations
104       though, or at least set "can()" appropriately.
105
106       There are also potential name collisions with methods in this module or
107       "Test::MockObject", though this should be rare.
108

AUTHOR

110       chromatic, <chromatic at wgz dot org>
111
112       Documentation bug fixed by Stevan Little.  Additional AUTOLOAD approach
113       suggested by Adam Kennedy.  Other bugs reported by Paul the Nomad and
114       Praveen Ray.  Thank you all!
115

BUGS

117       No known bugs.
118
120       Copyright (c) 2004 - 2006, chromatic.  All rights reserved.  You may
121       use, modify, and distribute this module under the same terms as Perl
122       5.8.x.
123
124
125
126perl v5.8.8                       2006-10-05      Test::MockObject::Extends(3)
Impressum