1Test::Stream::Plugin::MUoscekr(3C)ontributed Perl DocumeTnetsatt:i:oSntream::Plugin::Mock(3)
2
3
4
6 Test::Stream::Plugin::Mock - Class/Instance mocking for Test::Stream.
7
9 This distribution is deprecated in favor of Test2, Test2::Suite, and
10 Test2::Workflow.
11
12 See Test::Stream::Manual::ToTest2 for a conversion guide.
13
15 Mocking is often an essential part of testing. This library covers some
16 of the most common mocking needs. This plugin is heavily influenced by
17 Mock::Quick, but with an improved API. This plugin is also intended to
18 play well with other plugins in ways Mock::Quick would be unable to.
19
21 my $mock = mock 'Some::Class' => (
22 add => [
23 new_method => sub { ... },
24 ],
25 override => [
26 replace_method => sub { ... },
27 ],
28 );
29
30 Some::Class->new_method(); # Calls the newly injected method
31 Some::Class->replace_method(); # Calls our replacement method.
32
33 $mock->override(...) # Override some more
34
35 $mock = undef; # Undoes all the mocking, restoring all original methods.
36
38 This plugin plays nicely with Test::Stream::Plugin::Spec. Mocks are
39 treated as a "before_each" if you use the mock functions without saving
40 the returned object. The mock will also apply to any describe block in
41 which they are defined.
42
43 describe stuff => sub {
44 # The mock specification
45 mock 'My::Class' => (...);
46
47 # Mock applies here, inside the describe block
48
49 tests foo => sub {
50 # Mock applies here inside any nested blocks, even though they run
51 # later
52 };
53 };
54
55 # Mock does not apply out here
56
58 DEFAULT
59 mock
60 This is a 1-stop shop function that delgates to one of the other
61 methods depending on how it is used. If you are not comfortable
62 with a function that has a lot of potential behaviors, you can use
63 one of the other functions directly.
64
65 $mock = mocked($object)
66 $mock = mocked($class)
67 Check if an object or class is mocked. If it is mocked the $mock
68 object (Test::Stream::Mock) will be returned.
69
70 $mock = mock $class => ( ... );
71 $mock = mock $instance => ( ... )
72 $mock = mock 'class', $class => ( ... )
73 These forms delegate to mock_class() to mock a package. The third
74 form is to be explicit about what type of mocking you want.
75
76 $obj = mock()
77 $obj = mock { ... }
78 $obj = mock 'obj', ...;
79 These forms delegate to mock_obj() to create instances of anonymous
80 packages where methods are vivified into existance as needed.
81
82 mock $mock => sub { ... }
83 mock $method => ( ... )
84 These forms go together, the first form will set $mock as the
85 current mock build, then run the sub. Within the sub you can
86 declare mock specifications using the second form. The first form
87 delgates to mock_build().
88
89 The second form calls the specified method on the current build.
90 This second form delgates to mock_do().
91
92 BY REQUEST
93 DEFINING MOCKS
94
95 $obj = mock_obj( ... )
96 $obj = mock_obj { ... } => ( ... )
97 $obj = mock_obj sub { ... }
98 $obj = mock_obj { ... } => sub { ... }
99 This method lets you quickly generate a blessed object. The object
100 will be an instance of a randomly generated package name. Methods
101 will vivify as read/write accessors as needed.
102
103 Arguments can be any method available to Test::Stream::Mock
104 followed by an argument. If the very first argument is a hashref
105 then it will be 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 $mock = mock_class $class => ( ... )
111 $mock = mock_class $instance => ( ... )
112 $mock = mock_class ... => sub { ... }
113 This will create a new instance of Test::Stream::Mock to control
114 the package specified. If you give it a blessed reference it will
115 use the class of the instance.
116
117 Arguments can be any method available to Test::Stream::Mock
118 followed by an argument. If the very first argument is a hashref
119 then it will be blessed as your new object.
120
121 If you provide a coderef instead of key/value pairs, the coderef
122 will be run to build the mock. (See the "BUILDING MOCKS" section).
123
124 BUILDING MOCKS
125
126 mock_build $mock => sub { ... }
127 Set $mock as the current build, then run the specified code. $mock
128 will no longer be the current build when the sub is complete.
129
130 $mock = mock_building()
131 Get the current building $mock object.
132
133 mock_do $method => $args
134 Run the specified method on the currently building object.
135
136 METHOD GENERATORS
137
138 $sub = mock_accessor $field
139 Generate a read/write accessor for the specified field. This will
140 generate a sub like the following:
141
142 $sub = sub {
143 my $self = shift;
144 ($self->{$field}) = @_ if @_;
145 return $self->{$field};
146 };
147
148 $sub = mock_getter $field
149 Generate a read obly accessor for the specified field. This will
150 generate a sub like the following:
151
152 $sub = sub {
153 my $self = shift;
154 return $self->{$field};
155 };
156
157 $sub = mock_setter $field
158 Generate a write accessor for the specified field. This will
159 generate a sub like the following:
160
161 $sub = sub {
162 my $self = shift;
163 ($self->{$field}) = @_;
164 };
165
166 %pairs = mock_accessors(qw/name1 name2 name3/)
167 Generates several read/write accessors at once, returns key/value
168 pairs where the key is the field name, and the value is the
169 coderef.
170
171 %pairs = mock_getters(qw/name1 name2 name3/)
172 Generates several read only accessors at once, returns key/value
173 pairs where the key is the field name, and the value is the
174 coderef.
175
176 %pairs = mock_setters(qw/name1 name2 name3/)
177 Generates several write accessors at once, returns key/value pairs
178 where the key is the field name, and the value is the coderef.
179
181 my $mock = mock(...);
182
183 Mock objects are instances of Test::Stream::Mock, see it for their
184 methods.
185
187 The source code repository for Test::Stream can be found at
188 http://github.com/Test-More/Test-Stream/.
189
191 Chad Granum <exodist@cpan.org>
192
194 Chad Granum <exodist@cpan.org>
195
197 Copyright 2015 Chad Granum <exodist7@gmail.com>.
198
199 This program is free software; you can redistribute it and/or modify it
200 under the same terms as Perl itself.
201
202 See http://dev.perl.org/licenses/
203
204
205
206perl v5.36.0 2023-01-20 Test::Stream::Plugin::Mock(3)