Debian Codegraph @

upstream/debian-unstable/man3/Test2::Mock.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Test2::Mock 3perl"
58 .TH Test2::Mock 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Test2::Mock \- Module for managing mocked classes and instances.
65 .SH DESCRIPTION
66 .IX Header "DESCRIPTION"
67 This module lets you add and override methods for any package temporarily. When
68 the instance is destroyed it will restore the package to its original state.
69 .SH SYNOPSIS
70 .IX Header "SYNOPSIS"
71 .Vb 2
72 \& use Test2::Mock;
73 \& use MyClass;
74 \&
75 \& my $mock = Test2::Mock\->new(
76 \& track => $BOOL, # enable call tracking if desired
77 \& class => \*(AqMyClass\*(Aq,
78 \& override => [
79 \& name => sub { \*(Aqfred\*(Aq },
80 \& ...
81 \& ],
82 \& add => [
83 \& is_mocked => sub { 1 }
84 \& ...
85 \& ],
86 \& ...
87 \& );
88 \&
89 \& # Unmock the \*(Aqname\*(Aq sub
90 \& $mock\->restore(\*(Aqname\*(Aq);
91 \&
92 \& ...
93 \&
94 \& $mock = undef; # Will remove all the mocking
95 .Ve
96 .SH CONSTRUCTION
97 .IX Header "CONSTRUCTION"
98 .SH METHODS
99 .IX Header "METHODS"
100 .ie n .IP "$mock = Test2::Mock\->new(class => $CLASS, ...)" 4
101 .el .IP "\f(CW$mock\fR = Test2::Mock\->new(class => \f(CW$CLASS\fR, ...)" 4
102 .IX Item "$mock = Test2::Mock->new(class => $CLASS, ...)"
103 This will create a new instance of Test2::Mock that manages mocking
104 for the specified \f(CW$CLASS\fR.
105 .Sp
106 Any \f(CW\*(C`Test2::Mock\*(C'\fR method can be used as a constructor argument, each
107 should be followed by an arrayref of arguments to be used within the method. For
108 instance the \f(CWadd()\fR method:
109 .Sp
110 .Vb 4
111 \& my $mock = Test2::Mock\->new(
112 \& class => \*(AqAClass\*(Aq,
113 \& add => [foo => sub { \*(Aqfoo\*(Aq }],
114 \& );
115 .Ve
116 .Sp
117 is identical to this:
118 .Sp
119 .Vb 4
120 \& my $mock = Test2::Mock\->new(
121 \& class => \*(AqAClass\*(Aq,
122 \& );
123 \& $mock\->add(foo => sub { \*(Aqfoo\*(Aq });
124 .Ve
125 .ie n .IP $mock\->track($bool) 4
126 .el .IP \f(CW$mock\fR\->track($bool) 4
127 .IX Item "$mock->track($bool)"
128 Turn tracking on or off. Any sub added/overridden/set when tracking is on will
129 log every call in a hash retrievable via \f(CW\*(C`$mock\->tracking\*(C'\fR. Changing the
130 tracking toggle will not affect subs already altered, but will affect any
131 additional alterations.
132 .ie n .IP "$hashref = $mock\->sub_tracking" 4
133 .el .IP "\f(CW$hashref\fR = \f(CW$mock\fR\->sub_tracking" 4
134 .IX Item "$hashref = $mock->sub_tracking"
135 The tracking data looks like this:
136 .Sp
137 .Vb 7
138 \& {
139 \& sub_name => [
140 \& {sub_name => $sub_name, sub_ref => $mock_subref, args => [... copy of @_ from the call ... ]},
141 \& ...,
142 \& ...,
143 \& ],
144 \& }
145 .Ve
146 .Sp
147 Unlike call_tracking, this lists all calls by sub, so you can choose to only
148 look at the sub specific calls.
149 .Sp
150 \&\fBPlease note:\fR The hashref items with the subname and args are shared with
151 call_tracking, modifying one modifies the other, so copy first!
152 .ie n .IP "$arrayref = $mock\->call_tracking" 4
153 .el .IP "\f(CW$arrayref\fR = \f(CW$mock\fR\->call_tracking" 4
154 .IX Item "$arrayref = $mock->call_tracking"
155 The tracking data looks like this:
156 .Sp
157 .Vb 5
158 \& [
159 \& {sub_name => $sub_name, sub_ref => $mock_subref, args => [... copy of @_ from the call ... ]},
160 \& ...,
161 \& ...,
162 \& ]
163 .Ve
164 .Sp
165 Unlike sub_tracking this lists all calls to any mocked sub, in the order they
166 were called. To filter by sub use sub_tracking.
167 .Sp
168 \&\fBPlease note:\fR The hashref items with the subname and args are shared with
169 sub_tracking, modifying one modifies the other, so copy first!
170 .ie n .IP $mock\->\fBclear_sub_tracking()\fR 4
171 .el .IP \f(CW$mock\fR\->\fBclear_sub_tracking()\fR 4
172 .IX Item "$mock->clear_sub_tracking()"
173 .PD 0
174 .ie n .IP $mock\->clear_sub_tracking(\e@subnames) 4
175 .el .IP \f(CW$mock\fR\->clear_sub_tracking(\e@subnames) 4
176 .IX Item "$mock->clear_sub_tracking(@subnames)"
177 .PD
178 Clear tracking data. With no arguments ALL tracking data is cleared. When
179 arguments are provided then only those specific keys will be cleared.
180 .ie n .IP $mock\->\fBclear_call_tracking()\fR 4
181 .el .IP \f(CW$mock\fR\->\fBclear_call_tracking()\fR 4
182 .IX Item "$mock->clear_call_tracking()"
183 Clear all items from call_tracking.
184 .ie n .IP "$mock\->add('symbol' => ..., 'symbol2' => ...)" 4
185 .el .IP "\f(CW$mock\fR\->add('symbol' => ..., 'symbol2' => ...)" 4
186 .IX Item "$mock->add('symbol' => ..., 'symbol2' => ...)"
187 .PD 0
188 .ie n .IP "$mock\->override('symbol1' => ..., 'symbol2' => ...)" 4
189 .el .IP "\f(CW$mock\fR\->override('symbol1' => ..., 'symbol2' => ...)" 4
190 .IX Item "$mock->override('symbol1' => ..., 'symbol2' => ...)"
191 .ie n .IP "$mock\->set('symbol1' => ..., 'symbol2' => ...)" 4
192 .el .IP "\f(CW$mock\fR\->set('symbol1' => ..., 'symbol2' => ...)" 4
193 .IX Item "$mock->set('symbol1' => ..., 'symbol2' => ...)"
194 .PD
195 \&\f(CWadd()\fR and \f(CWoverride()\fR are the primary ways to add/modify methods for a
196 class. Both accept the exact same type of arguments. The difference is that
197 \&\f(CW\*(C`override\*(C'\fR will fail unless the symbol you are overriding already exists,
198 \&\f(CW\*(C`add\*(C'\fR on the other hand will fail if the symbol does already exist.
199 .Sp
200 \&\f(CWset()\fR was more recently added for cases where you may not know if the sub
201 already exists. These cases are rare, and set should be avoided (think of it
202 like 'no strict'). However there are valid use cases, so it was added.
203 .Sp
204 \&\fBNote:\fR Think of override as a push operation. If you call override on the
205 same symbol multiple times it will track that. You can use \f(CWrestore()\fR as a
206 pop operation to go back to the previous mock. \f(CW\*(C`reset\*(C'\fR can be used to remove
207 all the mocking for a symbol.
208 .Sp
209 Arguments must be a symbol name, with optional sigil, followed by a new
210 specification of the symbol. If no sigil is specified then '&' (sub) is
211 assumed. A simple example of overriding a sub:
212 .Sp
213 .Vb 3
214 \& $mock\->override(foo => sub { \*(Aqoverridden foo\*(Aq });
215 \& my $val = $class\->foo; # Runs our override
216 \& # $val is now set to \*(Aqoverridden foo\*(Aq
217 .Ve
218 .Sp
219 You can also simply provide a value and it will be wrapped in a sub for you:
220 .Sp
221 .Vb 1
222 \& $mock\->override( foo => \*(Aqfoo\*(Aq );
223 .Ve
224 .Sp
225 The example above will generate a sub that always returns the string 'foo'.
226 .Sp
227 There are three *special* values that can be used to generate accessors:
228 .Sp
229 .Vb 5
230 \& $mock\->add(
231 \& name => \*(Aqrw\*(Aq, # Generates a read/write accessor
232 \& age => \*(Aqro\*(Aq, # Generates a read only accessor
233 \& size => \*(Aqwo\*(Aq, # Generates a write only accessor
234 \& );
235 .Ve
236 .Sp
237 If you want to have a sub that actually returns one of the three special strings, or
238 that returns a coderef, you can use a hashref as the spec:
239 .Sp
240 .Vb 7
241 \& my $ref = sub { \*(Aqmy sub\*(Aq };
242 \& $mock\->add(
243 \& rw_string => { val => \*(Aqrw\*(Aq },
244 \& ro_string => { val => \*(Aqro\*(Aq },
245 \& wo_string => { val => \*(Aqwo\*(Aq },
246 \& coderef => { val => $ref }, # the coderef method returns $ref each time
247 \& );
248 .Ve
249 .Sp
250 You can also override/add other symbol types, such as hash:
251 .Sp
252 .Vb 2
253 \& package Foo;
254 \& ...
255 \&
256 \& $mock\->add(\*(Aq%foo\*(Aq => {a => 1});
257 \&
258 \& print $Foo::foo{a}; # prints \*(Aq1\*(Aq
259 .Ve
260 .Sp
261 You can also tell mock to deduce the symbol type for the add/override from the
262 reference, rules are similar to glob assignments:
263 .Sp
264 .Vb 6
265 \& $mock\->add(
266 \& \-foo => sub { \*(Aqfoo\*(Aq }, # Adds the &foo sub to the package
267 \& \-foo => { foo => 1 }, # Adds the %foo hash to the package
268 \& \-foo => [ \*(Aqf\*(Aq, \*(Aqo\*(Aq, \*(Aqo\*(Aq ], # Adds the @foo array to the package
269 \& \-foo => \e"foo", # Adds the $foo scalar to the package
270 \& );
271 .Ve
272 .ie n .IP $mock\->restore($SYMBOL) 4
273 .el .IP \f(CW$mock\fR\->restore($SYMBOL) 4
274 .IX Item "$mock->restore($SYMBOL)"
275 Restore the symbol to what it was before the last override. If the symbol was
276 recently added this will remove it. If the symbol has been overridden multiple
277 times this will ONLY restore it to the previous state. Think of \f(CW\*(C`override\*(C'\fR as a
278 push operation, and \f(CW\*(C`restore\*(C'\fR as the pop operation.
279 .ie n .IP $mock\->reset($SYMBOL) 4
280 .el .IP \f(CW$mock\fR\->reset($SYMBOL) 4
281 .IX Item "$mock->reset($SYMBOL)"
282 Remove all mocking of the symbol and restore the original symbol. If the symbol
283 was initially added then it will be completely removed.
284 .ie n .IP $mock\->orig($SYMBOL) 4
285 .el .IP \f(CW$mock\fR\->orig($SYMBOL) 4
286 .IX Item "$mock->orig($SYMBOL)"
287 This will return the original symbol, before any mocking. For symbols that were
288 added this will return undef.
289 .ie n .IP $mock\->current($SYMBOL) 4
290 .el .IP \f(CW$mock\fR\->current($SYMBOL) 4
291 .IX Item "$mock->current($SYMBOL)"
292 This will return the current symbol.
293 .ie n .IP $mock\->reset_all 4
294 .el .IP \f(CW$mock\fR\->reset_all 4
295 .IX Item "$mock->reset_all"
296 Remove all added symbols, and restore all overridden symbols to their originals.
297 .ie n .IP "$mock\->add_constructor($NAME => $TYPE)" 4
298 .el .IP "\f(CW$mock\fR\->add_constructor($NAME => \f(CW$TYPE\fR)" 4
299 .IX Item "$mock->add_constructor($NAME => $TYPE)"
300 .PD 0
301 .ie n .IP "$mock\->override_constructor($NAME => $TYPE)" 4
302 .el .IP "\f(CW$mock\fR\->override_constructor($NAME => \f(CW$TYPE\fR)" 4
303 .IX Item "$mock->override_constructor($NAME => $TYPE)"
304 .PD
305 This can be used to inject constructors. The first argument should be the name
306 of the constructor. The second argument specifies the constructor type.
307 .Sp
308 The \f(CW\*(C`hash\*(C'\fR type is the most common, all arguments are used to create a new
309 hash that is blessed.
310 .Sp
311 .Vb 4
312 \& hash => sub {
313 \& my ($class, %params) = @_;
314 \& return bless \e%params, $class;
315 \& };
316 .Ve
317 .Sp
318 The \f(CW\*(C`array\*(C'\fR type is similar to the hash type, but accepts a list instead of
319 key/value pairs:
320 .Sp
321 .Vb 4
322 \& array => sub {
323 \& my ($class, @params) = @_;
324 \& return bless \e@params, $class;
325 \& };
326 .Ve
327 .Sp
328 The \f(CW\*(C`ref\*(C'\fR type takes a reference and blesses it. This will modify your
329 original input argument.
330 .Sp
331 .Vb 4
332 \& ref => sub {
333 \& my ($class, $params) = @_;
334 \& return bless $params, $class;
335 \& };
336 .Ve
337 .Sp
338 The \f(CW\*(C`ref_copy\*(C'\fR type will copy your reference and bless the copy:
339 .Sp
340 .Vb 3
341 \& ref_copy => sub {
342 \& my ($class, $params) = @_;
343 \& my $type = reftype($params);
344 \&
345 \& return bless {%$params}, $class
346 \& if $type eq \*(AqHASH\*(Aq;
347 \&
348 \& return bless [@$params], $class
349 \& if $type eq \*(AqARRAY\*(Aq;
350 \&
351 \& croak "Not sure how to construct a \*(Aq$class\*(Aq from \*(Aq$params\*(Aq";
352 \& };
353 .Ve
354 .ie n .IP "$mock\->before($NAME, sub { ... })" 4
355 .el .IP "\f(CW$mock\fR\->before($NAME, sub { ... })" 4
356 .IX Item "$mock->before($NAME, sub { ... })"
357 This will replace the original sub \f(CW$NAME\fR with a new sub that calls your
358 custom code just before calling the original method. The return from your
359 custom sub is ignored. Your sub and the original both get the unmodified
360 arguments.
361 .ie n .IP "$mock\->after($NAME, sub { ... })" 4
362 .el .IP "\f(CW$mock\fR\->after($NAME, sub { ... })" 4
363 .IX Item "$mock->after($NAME, sub { ... })"
364 This is similar to before, except your callback runs after the original code.
365 The return from your callback is ignored.
366 .ie n .IP "$mock\->around($NAME, sub { ... })" 4
367 .el .IP "\f(CW$mock\fR\->around($NAME, sub { ... })" 4
368 .IX Item "$mock->around($NAME, sub { ... })"
369 This gives you the chance to wrap the original sub:
370 .Sp
371 .Vb 4
372 \& $mock\->around(foo => sub {
373 \& my $orig = shift;
374 \& my $self = shift;
375 \& my (@args) = @_;
376 \&
377 \& ...
378 \& $self\->$orig(@args);
379 \& ...
380 \&
381 \& return ...;
382 \& });
383 .Ve
384 .Sp
385 The original sub is passed in as the first argument, even before \f(CW$self\fR. You
386 are responsible for making sure your wrapper sub returns the correct thing.
387 .ie n .IP $mock\->autoload 4
388 .el .IP \f(CW$mock\fR\->autoload 4
389 .IX Item "$mock->autoload"
390 This will inject an \f(CW\*(C`AUTOLOAD\*(C'\fR sub into the class. This autoload will
391 automatically generate read-write accessors for any sub called that does not
392 already exist.
393 .ie n .IP $mock\->block_load 4
394 .el .IP \f(CW$mock\fR\->block_load 4
395 .IX Item "$mock->block_load"
396 This will prevent the real class from loading until the mock is destroyed. This
397 will fail if the class is already loaded. This will let you mock a class
398 completely without loading the original module.
399 .ie n .IP "$pm_file = $mock\->file" 4
400 .el .IP "\f(CW$pm_file\fR = \f(CW$mock\fR\->file" 4
401 .IX Item "$pm_file = $mock->file"
402 This returns the relative path to the file for the module. This corresponds to
403 the \f(CW%INC\fR entry.
404 .ie n .IP "$bool = $mock\->purge_on_destroy($bool)" 4
405 .el .IP "\f(CW$bool\fR = \f(CW$mock\fR\->purge_on_destroy($bool)" 4
406 .IX Item "$bool = $mock->purge_on_destroy($bool)"
407 When true, this will cause the package stash to be completely obliterated when
408 the mock object falls out of scope or is otherwise destroyed. You do not
409 normally want this.
410 .ie n .IP "$stash = $mock\->stash" 4
411 .el .IP "\f(CW$stash\fR = \f(CW$mock\fR\->stash" 4
412 .IX Item "$stash = $mock->stash"
413 This returns the stash for the class being mocked. This is the equivalent of:
414 .Sp
415 .Vb 1
416 \& my $stash = \e%{"${class}\e::"};
417 .Ve
418 .Sp
419 This saves you from needing to turn off strict.
420 .ie n .IP "$class = $mock\->class" 4
421 .el .IP "\f(CW$class\fR = \f(CW$mock\fR\->class" 4
422 .IX Item "$class = $mock->class"
423 The class being mocked by this instance.
424 .ie n .IP "$p = $mock\->parent" 4
425 .el .IP "\f(CW$p\fR = \f(CW$mock\fR\->parent" 4
426 .IX Item "$p = $mock->parent"
427 If you mock a class twice the first instance is the parent, the second is the
428 child. This prevents the parent from being destroyed before the child, which
429 would lead to a very unpleasant situation.
430 .ie n .IP "$c = $mock\->child" 4
431 .el .IP "\f(CW$c\fR = \f(CW$mock\fR\->child" 4
432 .IX Item "$c = $mock->child"
433 Returns the child mock, if any.
434 .SH SOURCE
435 .IX Header "SOURCE"
436 The source code repository for Test2\-Suite can be found at
437 <https://github.com/Test\-More/Test2\-Suite/>.
438 .SH MAINTAINERS
439 .IX Header "MAINTAINERS"
440 .IP "Chad Granum <[email protected]>" 4
441 .IX Item "Chad Granum <[email protected]>"
442 .SH AUTHORS
443 .IX Header "AUTHORS"
444 .PD 0
445 .IP "Chad Granum <[email protected]>" 4
446 .IX Item "Chad Granum <[email protected]>"
447 .PD
448 .SH COPYRIGHT
449 .IX Header "COPYRIGHT"
450 Copyright 2018 Chad Granum <[email protected]>.
451 .PP
452 This program is free software; you can redistribute it and/or
453 modify it under the same terms as Perl itself.
454 .PP
455 See <https://dev.perl.org/licenses/>