1Mock::Quick::Class(3) User Contributed Perl DocumentationMock::Quick::Class(3)
2
3
4
6 Mock::Quick::Class - Class mocking for Mock::Quick
7
9 Provides class mocking for Mock::Quick
10
12 IMPLEMENT A CLASS
13 This will implement a class at the namespace provided via the
14 -implement argument. The class must not already be loaded. Once
15 complete the real class will be prevented from loading until you call
16 undefine() on the control object.
17
18 use Mock::Quick::Class;
19
20 my $control = Mock::Quick::Class->new(
21 -implement => 'My::Package',
22
23 # Insert a generic new() method (blessed hash)
24 -with_new => 1,
25
26 # Inheritance
27 -subclass => 'Some::Class',
28 # Can also do
29 -subclass => [ 'Class::A', 'Class::B' ],
30
31 # generic get/set attribute methods.
32 -attributes => [ qw/a b c d/ ],
33
34 # Method that simply returns a value.
35 simple => 'value',
36
37 # Custom method.
38 method => sub { ... },
39 );
40
41 my $obj = $control->package->new;
42 # OR
43 my $obj = My::Package->new;
44
45 # Override a method
46 $control->override( foo => sub { ... });
47
48 # Restore it to the original
49 $control->restore( 'foo' );
50
51 # Remove the namespace we created, which would allow the real thing to load
52 # in a require or use statement.
53 $control->undefine();
54
55 You can also use the 'implement' method instead of new:
56
57 use Mock::Quick::Class;
58
59 my $control = Mock::Quick::Class->implement(
60 'Some::Package',
61 %args
62 );
63
64 ANONYMOUS MOCKED CLASS
65 This is if you just need to generate a class where the package name
66 does not matter. This is done when the -takeover and -implement
67 arguments are both omitted.
68
69 use Mock::Quick::Class;
70
71 my $control = Mock::Quick::Class->new(
72 # Insert a generic new() method (blessed hash)
73 -with_new => 1,
74
75 # Inheritance
76 -subclass => 'Some::Class',
77 # Can also do
78 -subclass => [ 'Class::A', 'Class::B' ],
79
80 # generic get/set attribute methods.
81 -attributes => [ qw/a b c d/ ],
82
83 # Method that simply returns a value.
84 simple => 'value',
85
86 # Custom method.
87 method => sub { ... },
88 );
89
90 my $obj = $control->package->new;
91
92 # Override a method
93 $control->override( foo => sub { ... });
94
95 # Restore it to the original
96 $control->restore( 'foo' );
97
98 # Remove the anonymous namespace we created.
99 $control->undefine();
100
101 TAKING OVER EXISTING/LOADED CLASSES
102 use Mock::Quick::Class;
103
104 my $control = Mock::Quick::Class->takeover( 'Some::Package' );
105
106 # Override a method
107 $control->override( foo => sub { ... });
108
109 # Restore it to the original
110 $control->restore( 'foo' );
111
112 # Destroy the control object and completely restore the original class
113 # Some::Package.
114 $control = undef;
115
116 You can also do this through new()
117
118 use Mock::Quick::Class;
119
120 my $control = Mock::Quick::Class->new(
121 -takeover => 'Some::Package',
122 %overrides
123 );
124
126 While the control object exists, it can be accessed via
127 "YOUR::PACKAGE-"MQ_CONTROL()>. It is important to note that this method
128 will disappear whenever the control object you track falls out of
129 scope.
130
131 Example (taken from Class.t):
132
133 $obj = $CLASS->new( -takeover => 'Baz' );
134 $obj->override( 'foo', sub {
135 my $class = shift;
136 return "PREFIX: " . $class->MQ_CONTROL->original( 'foo' )->();
137 });
138
139 is( Baz->foo, "PREFIX: foo", "Override and accessed original through MQ_CONTROL" );
140 $obj = undef;
141
142 is( Baz->foo, 'foo', 'original' );
143 ok( !Baz->can('MQ_CONTROL'), "Removed control" );
144
146 $package = $obj->package()
147 Get the name of the package controlled by this object.
148
149 $bool = $obj->is_takeover()
150 Check if the control object was created to takeover an existing
151 class.
152
153 $bool = $obj->is_implement()
154 Check if the control object was created to implement a class.
155
156 $data = $obj->metrics()
157 Returns a hash where keys are method names, and values are the
158 number of times the method has been called. When a method is
159 altered or removed the key is deleted.
160
161 $obj->override( name => sub { ... })
162 Override a method.
163
164 $obj->original( $name );
165 Get the original method (coderef). Note: The first time this is
166 called it find and remembers the value of package->can( $name ).
167 This means that if you modify or replace the method without using
168 Mock::Quick before this is called, it will have the updated method,
169 not the true original.
170
171 The override() method will call this first to ensure the original
172 method is cached and available for restore(). Once a value is set
173 it is never replaced or cleared.
174
175 $obj->restore( $name )
176 Restore a method (Resets metrics)
177
178 $obj->undefine()
179 Undefine the package controlled by the control.
180
182 Chad Granum exodist7@gmail.com
183 Glen Hinkle glen@empireenterprises.com
184
186 Copyright (C) 2011 Chad Granum
187
188 Mock-Quick is free software; Standard perl licence.
189
190 Mock-Quick is distributed in the hope that it will be useful, but
191 WITHOUT ANY WARRANTY; without even the implied warranty of
192 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license
193 for more details.
194
195
196
197perl v5.32.0 2020-07-28 Mock::Quick::Class(3)