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               add => [
17                   new_method => sub { ... },
18               ],
19               override => [
20                   replace_method => sub { ... },
21               ],
22               set => [
23                   replace_or_inject => sub { ... },
24               ]
25           );
26
27           Some::Class->new_method();        # Calls the newly injected method
28           Some::Class->replace_method();    # Calls our replacement method.
29
30           $mock->override(...) # Override some more
31
32           $mock = undef; # Undoes all the mocking, restoring all original methods.
33
34           my $simple_mock = mock {} => (
35               add => [
36                   is_active => sub { ... }
37               ]
38           );
39
40           $simple_mock->is_active();        # Calls our newly mocked method.
41

EXPORTS

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

MOCK CONTROL OBJECTS

167           my $mock = mock(...);
168
169       Mock objects are instances of Test2::Mock. See it for their methods.
170

SOURCE

172       The source code repository for Test2-Suite can be found at
173       https://github.com/Test-More/Test2-Suite/.
174

MAINTAINERS

176       Chad Granum <exodist@cpan.org>
177

AUTHORS

179       Chad Granum <exodist@cpan.org>
180
182       Copyright 2018 Chad Granum <exodist@cpan.org>.
183
184       This program is free software; you can redistribute it and/or modify it
185       under the same terms as Perl itself.
186
187       See http://dev.perl.org/licenses/
188
189
190
191perl v5.28.1                      2018-12-04             Test2::Tools::Mock(3)
Impressum