1Test::Stream::Plugin::MUoscekr(3C)ontributed Perl DocumeTnetsatt:i:oSntream::Plugin::Mock(3)
2
3
4

NAME

6       Test::Stream::Plugin::Mock - Class/Instance mocking for Test::Stream.
7

DEPRECATED

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

DESCRIPTION

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

SYNOPSIS

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

MOCKING + SPEC TESTING

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

EXPORTS

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
80           anonymous packages where methods are vivified into existance as
81           needed.
82
83       mock $mock => sub { ... }
84       mock $method => ( ... )
85           These forms go together, the first form will set $mock as the
86           current mock build, then run the sub. Within the sub you can
87           declare mock specifications using the second form. The first form
88           delgates to "mock_build()".
89
90           The second form calls the specified method on the current build.
91           This second form delgates to "mock_do()".
92
93   BY REQUEST
94       DEFINING MOCKS
95
96       $obj = mock_obj( ... )
97       $obj = mock_obj { ... } => ( ... )
98       $obj = mock_obj sub { ... }
99       $obj = mock_obj { ... } => sub { ... }
100           This method lets you quickly generate a blessed object. The object
101           will be an instance of a randomly generated package name. Methods
102           will vivify as read/write accessors as needed.
103
104           Arguments can be any method available to Test::Stream::Mock
105           followed by an argument. If the very first argument is a hashref
106           then it will be blessed as your new object.
107
108           If you provide a coderef instead of key/value pairs, the coderef
109           will be run to build the mock. (See the "BUILDING MOCKS" section).
110
111       $mock = mock_class $class => ( ... )
112       $mock = mock_class $instance => ( ... )
113       $mock = mock_class ... => sub { ... }
114           This will create a new instance of Test::Stream::Mock to control
115           the package specified. If you give it a blessed reference it will
116           use the class of the instance.
117
118           Arguments can be any method available to Test::Stream::Mock
119           followed by an argument. If the very first argument is a hashref
120           then it will be blessed as your new object.
121
122           If you provide a coderef instead of key/value pairs, the coderef
123           will be run to build the mock. (See the "BUILDING MOCKS" section).
124
125       BUILDING MOCKS
126
127       mock_build $mock => sub { ... }
128           Set $mock as the current build, then run the specified code. $mock
129           will no longer be the current build when the sub is complete.
130
131       $mock = mock_building()
132           Get the current building $mock object.
133
134       mock_do $method => $args
135           Run the specified method on the currently building object.
136
137       METHOD GENERATORS
138
139       $sub = mock_accessor $field
140           Generate a read/write accessor for the specified field. This will
141           generate a sub like the following:
142
143               $sub = sub {
144                   my $self = shift;
145                   ($self->{$field}) = @_ if @_;
146                   return $self->{$field};
147               };
148
149       $sub = mock_getter $field
150           Generate a read obly accessor for the specified field. This will
151           generate a sub like the following:
152
153               $sub = sub {
154                   my $self = shift;
155                   return $self->{$field};
156               };
157
158       $sub = mock_setter $field
159           Generate a write accessor for the specified field. This will
160           generate a sub like the following:
161
162               $sub = sub {
163                   my $self = shift;
164                   ($self->{$field}) = @_;
165               };
166
167       %pairs = mock_accessors(qw/name1 name2 name3/)
168           Generates several read/write accessors at once, returns key/value
169           pairs where the key is the field name, and the value is the
170           coderef.
171
172       %pairs = mock_getters(qw/name1 name2 name3/)
173           Generates several read only accessors at once, returns key/value
174           pairs where the key is the field name, and the value is the
175           coderef.
176
177       %pairs = mock_setters(qw/name1 name2 name3/)
178           Generates several write accessors at once, returns key/value pairs
179           where the key is the field name, and the value is the coderef.
180

MOCK CONTROL OBJECTS

182           my $mock = mock(...);
183
184       Mock objects are instances of Test::Stream::Mock, see it for their
185       methods.
186

SOURCE

188       The source code repository for Test::Stream can be found at
189       http://github.com/Test-More/Test-Stream/.
190

MAINTAINERS

192       Chad Granum <exodist@cpan.org>
193

AUTHORS

195       Chad Granum <exodist@cpan.org>
196
198       Copyright 2015 Chad Granum <exodist7@gmail.com>.
199
200       This program is free software; you can redistribute it and/or modify it
201       under the same terms as Perl itself.
202
203       See http://dev.perl.org/licenses/
204
205
206
207perl v5.32.0                      2020-07-28     Test::Stream::Plugin::Mock(3)
Impressum