1Test2::Tools::Mock(3) User Contributed Perl DocumentationTest2::Tools::Mock(3)
2
3
4

NAME

6       Test2::Tools::Mock - Class/Instance mocking for Test2.
7

DESCRIPTION

9       Mocking is often an essential part of testing. This library covers some
10       of the most common mocking needs. This plugin is heavily influenced by
11       Mock::Quick, but with an improved API. This plugin is also intended to
12       play well with other plugins in ways Mock::Quick would be unable to.
13

SYNOPSIS

15           my $mock = mock 'Some::Class' => (
16               track => $BOOL, # Enable/Disable tracking on subs defined below
17
18               add => [
19                   new_method => sub { ... },
20               ],
21               override => [
22                   replace_method => sub { ... },
23               ],
24               set => [
25                   replace_or_inject => sub { ... },
26               ],
27
28               track => $bool, # enable/disable tracking again to affect mocks made after this point
29               ..., # Argument keys may be repeated
30           );
31
32           Some::Class->new_method();        # Calls the newly injected method
33           Some::Class->replace_method();    # Calls our replacement method.
34
35           $mock->override(...) # Override some more
36
37           $mock = undef; # Undoes all the mocking, restoring all original methods.
38
39           my $simple_mock = mock {} => (
40               add => [
41                   is_active => sub { ... }
42               ]
43           );
44
45           $simple_mock->is_active();        # Calls our newly mocked method.
46

EXPORTS

48   DEFAULT
49       mock
50           This is a one-stop shop function that delegates to one of the other
51           methods depending on how it is used. If you are not comfortable
52           with a function that has a lot of potential behaviors, you can use
53           one of the other functions directly.
54
55       @mocks = mocked($object)
56       @mocks = mocked($class)
57           Check if an object or class is mocked. If it is mocked the $mock
58           object(s) (Test2::Mock) will be returned.
59
60       $mock = mock $class => ( ... );
61       $mock = mock $instance => ( ... )
62       $mock = mock 'class', $class => ( ... )
63           These forms delegate to "mock_class()" to mock a package. The third
64           form is to be explicit about what type of mocking you want.
65
66       $obj = mock()
67       $obj = mock { ... }
68       $obj = mock 'obj', ...;
69           These forms delegate to "mock_obj()" to create instances of
70           anonymous packages where methods are vivified into existence as
71           needed.
72
73       mock $mock => sub { ... }
74       mock $method => ( ... )
75           These forms go together, the first form will set $mock as the
76           current mock build, then run the sub. Within the sub you can
77           declare mock specifications using the second form. The first form
78           delegates to "mock_build()".
79
80           The second form calls the specified method on the current build.
81           This second form delegates to "mock_do()".
82
83   BY REQUEST
84       DEFINING MOCKS
85
86       $obj = mock_obj( ... )
87       $obj = mock_obj { ... } => ( ... )
88       $obj = mock_obj sub { ... }
89       $obj = mock_obj { ... } => sub { ... }
90           This method lets you quickly generate a blessed object. The object
91           will be an instance of a randomly generated package name. Methods
92           will vivify as read/write accessors as needed.
93
94           Arguments can be any method available to Test2::Mock followed by an
95           argument. If the very first argument is a hashref then it will be
96           blessed as your new object.
97
98           If you provide a coderef instead of key/value pairs, the coderef
99           will be run to build the mock. (See the "BUILDING MOCKS" section).
100
101       $mock = mock_class $class => ( ... )
102       $mock = mock_class $instance => ( ... )
103       $mock = mock_class ... => sub { ... }
104           This will create a new instance of Test2::Mock to control the
105           package specified. If you give it a blessed reference it will use
106           the class of the instance.
107
108           Arguments can be any method available to Test2::Mock followed by an
109           argument. If the very first argument is a hashref then it will be
110           blessed as your new object.
111
112           If you provide a coderef instead of key/value pairs, the coderef
113           will be run to build the mock. (See the "BUILDING MOCKS" section).
114
115       BUILDING MOCKS
116
117       mock_build $mock => sub { ... }
118           Set $mock as the current build, then run the specified code. $mock
119           will no longer be the current build when the sub is complete.
120
121       $mock = mock_building()
122           Get the current building $mock object.
123
124       mock_do $method => $args
125           Run the specified method on the currently building object.
126
127       METHOD GENERATORS
128
129       $sub = mock_accessor $field
130           Generate a read/write accessor for the specified field. This will
131           generate a sub like the following:
132
133               $sub = sub {
134                   my $self = shift;
135                   ($self->{$field}) = @_ if @_;
136                   return $self->{$field};
137               };
138
139       $sub = mock_getter $field
140           Generate a read only accessor for the specified field. This will
141           generate a sub like the following:
142
143               $sub = sub {
144                   my $self = shift;
145                   return $self->{$field};
146               };
147
148       $sub = mock_setter $field
149           Generate a write accessor for the specified field. This will
150           generate a sub like the following:
151
152               $sub = sub {
153                   my $self = shift;
154                   ($self->{$field}) = @_;
155               };
156
157       %pairs = mock_accessors(qw/name1 name2 name3/)
158           Generates several read/write accessors at once, returns key/value
159           pairs where the key is the field name, and the value is the
160           coderef.
161
162       %pairs = mock_getters(qw/name1 name2 name3/)
163           Generates several read only accessors at once, returns key/value
164           pairs where the key is the field name, and the value is the
165           coderef.
166
167       %pairs = mock_setters(qw/name1 name2 name3/)
168           Generates several write accessors at once, returns key/value pairs
169           where the key is the field name, and the value is the coderef.
170

MOCK CONTROL OBJECTS

172           my $mock = mock(...);
173
174       Mock objects are instances of Test2::Mock. See it for their methods.
175

SOURCE

177       The source code repository for Test2-Suite can be found at
178       <https://github.com/Test-More/Test2-Suite/>.
179

MAINTAINERS

181       Chad Granum <exodist@cpan.org>
182

AUTHORS

184       Chad Granum <exodist@cpan.org>
185
187       Copyright 2018 Chad Granum <exodist@cpan.org>.
188
189       This program is free software; you can redistribute it and/or modify it
190       under the same terms as Perl itself.
191
192       See <https://dev.perl.org/licenses/>
193
194
195
196perl v5.30.1                      2020-01-31             Test2::Tools::Mock(3)
Impressum