1Test::Mock::Guard(3)  User Contributed Perl Documentation Test::Mock::Guard(3)
2
3
4

NAME

6       Test::Mock::Guard - Simple mock test library using RAII.
7

SYNOPSIS

9         use Test::More;
10         use Test::Mock::Guard qw(mock_guard);
11
12         package Some::Class;
13
14         sub new { bless {} => shift }
15         sub foo { "foo" }
16         sub bar { 1; }
17
18         package main;
19
20         {
21             my $guard = mock_guard( 'Some::Class', { foo => sub { "bar" }, bar => 10 } );
22             my $obj = Some::Class->new;
23             is( $obj->foo, "bar" );
24             is( $obj->bar, 10 );
25         }
26
27         my $obj = Some::Class->new;
28         is( $obj->foo, "foo" );
29         is( $obj->bar, 1 );
30
31         done_testing;
32

DESCRIPTION

34       Test::Mock::Guard is mock test library using RAII.  This module is able
35       to change method behavior by each scope. See SYNOPSIS's sample code.
36

EXPORT FUNCTION

38   mock_guard( @class_defs )
39       @class_defs have the following format.
40
41       key Class name or object to mock.
42
43       value
44           Hash reference. Keys are method names; values are code references
45           or scalars.  If the value is code reference, it is used as a
46           method.  If the value is a scalar, the method will return the
47           specified value.
48
49       You can mock instance methods as well as class methods (this feature
50       was provided by cho45):
51
52         use Test::More;
53         use Test::Mock::Guard qw(mock_guard);
54
55         package Some::Class;
56
57         sub new { bless {} => shift }
58         sub foo { "foo" }
59
60         package main;
61
62         my $obj1 = Some::Class->new;
63         my $obj2 = Some::Class->new;
64
65         {
66             my $obj2 = Some::Class->new;
67             my $guard = mock_guard( $obj2, { foo => sub { "bar" } } );
68             is ($obj1->foo, "foo", "obj1 has not changed" );
69             is( $obj2->foo, "bar", "obj2 is mocked" );
70         }
71
72         is( $obj1->foo, "foo", "obj1" );
73         is( $obj2->foo, "foo", "obj2" );
74
75         done_testing;
76

METHODS

78   new( @class_defs )
79       See "mock_guard" definition.
80
81   call_count( $class_name_or_object, $method_name )
82       Returns a number of calling of $method_name in $class_name_or_object.
83

AUTHOR

85       Toru Yamaguchi <zigorou@cpan.org>
86
87       Yuji Shimada <xaicron at cpan.org>
88
89       Masaki Nakagawa <masaki@cpan.org>
90

THANKS TO

92       cho45 <cho45@lowreal.net>
93

SEE ALSO

95       Test::MockObject
96

LICENSE

98       This library is free software; you can redistribute it and/or modify it
99       under the same terms as Perl itself.
100
101
102
103perl v5.32.1                      2021-01-27              Test::Mock::Guard(3)
Impressum