1Test::MockObject::ExtenUdsse(r3)Contributed Perl DocumenTteastti:o:nMockObject::Extends(3)
2
3
4
6 Test::MockObject::Extends - mock part of an object or class
7
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
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
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
41 partially.
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
68 To do its magic, this module uses several internal methods:
69
70 · "check_class_loaded( $parent_class )"
71
72 This verifies that you have the mockee defined. If not, it attempts
73 to load the corresponding module for you.
74
75 · "gen_autoload( $extended )"
76
77 Returns an AUTOLOAD subroutine for the mock object that checks that
78 the extended object (or class) can perform the requested method,
79 that Test::MockObject can perform it, or that the parent has an
80 appropriate AUTOLOAD of its own. (It should have its own "can()" in
81 that case too though.)
82
83 · "gen_can( $extended )"
84
85 Returns a "can()" method for the mock object that respects the same
86 execution order as "gen_autoload()".
87
88 · "gen_isa( $extended )"
89
90 Returns an "isa()" method for the mock object that claims to be the
91 $extended object appropriately.
92
93 · "gen_get_parents( $extended )"
94
95 Returns a "__get_parents()" method for the mock object that claims
96 to be the $extended object appropriately.
97
98 · "gen_package( $extended )"
99
100 Creates a new unique package for the mock object with the
101 appropriate methods already installed.
102
103 · "get_class( $invocant )"
104
105 Returns the class name of the invocant, whether it's an object or a
106 class name.
107
109 There may be some weird corner cases with dynamically generated methods
110 in the mocked class. You really should use subroutine declarations
111 though, or at least set "can()" appropriately.
112
113 There are also potential name collisions with methods in this module or
114 "Test::MockObject", though this should be rare.
115
117 chromatic, <chromatic at wgz dot org>
118
119 Documentation bug fixed by Stevan Little. Additional AUTOLOAD approach
120 suggested by Adam Kennedy. Field-based objects supported by Gavin
121 Mogan. Other bugs reported by Paul the Nomad and Praveen Ray. Thank you
122 all!
123
125 No known bugs.
126
128 Copyright (c) 2004 - 2014, chromatic. All rights reserved. You may use,
129 modify, and distribute this module under the same terms as Perl 5.10
130
131
132
133perl v5.28.0 2018-07-05 Test::MockObject::Extends(3)