.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test::Mock::Guard 3pm" .TH Test::Mock::Guard 3pm 2023-07-26 "perl v5.38.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Test::Mock::Guard \- Simple mock test library using RAII. .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& use Test::More; \& use Test::Mock::Guard qw(mock_guard); \& \& package Some::Class; \& \& sub new { bless {} => shift } \& sub foo { "foo" } \& sub bar { 1; } \& \& package main; \& \& { \& my $guard = mock_guard( \*(AqSome::Class\*(Aq, { foo => sub { "bar" }, bar => 10 } ); \& my $obj = Some::Class\->new; \& is( $obj\->foo, "bar" ); \& is( $obj\->bar, 10 ); \& } \& \& my $obj = Some::Class\->new; \& is( $obj\->foo, "foo" ); \& is( $obj\->bar, 1 ); \& \& done_testing; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Test::Mock::Guard is mock test library using RAII. This module is able to change method behavior by each scope. See SYNOPSIS's sample code. .SH "EXPORT FUNCTION" .IX Header "EXPORT FUNCTION" .ie n .SS "mock_guard( @class_defs )" .el .SS "mock_guard( \f(CW@class_defs\fP )" .IX Subsection "mock_guard( @class_defs )" \&\f(CW@class_defs\fR have the following format. .IP key 4 .IX Item "key" Class name or object to mock. .IP value 4 .IX Item "value" Hash reference. Keys are method names; values are code references or scalars. If the value is code reference, it is used as a method. If the value is a scalar, the method will return the specified value. .PP You can mock instance methods as well as class methods (this feature was provided by cho45): .PP .Vb 2 \& use Test::More; \& use Test::Mock::Guard qw(mock_guard); \& \& package Some::Class; \& \& sub new { bless {} => shift } \& sub foo { "foo" } \& \& package main; \& \& my $obj1 = Some::Class\->new; \& my $obj2 = Some::Class\->new; \& \& { \& my $obj2 = Some::Class\->new; \& my $guard = mock_guard( $obj2, { foo => sub { "bar" } } ); \& is ($obj1\->foo, "foo", "obj1 has not changed" ); \& is( $obj2\->foo, "bar", "obj2 is mocked" ); \& } \& \& is( $obj1\->foo, "foo", "obj1" ); \& is( $obj2\->foo, "foo", "obj2" ); \& \& done_testing; .Ve .SH METHODS .IX Header "METHODS" .ie n .SS "new( @class_defs )" .el .SS "new( \f(CW@class_defs\fP )" .IX Subsection "new( @class_defs )" See "mock_guard" definition. .ie n .SS "call_count( $class_name_or_object, $method_name )" .el .SS "call_count( \f(CW$class_name_or_object\fP, \f(CW$method_name\fP )" .IX Subsection "call_count( $class_name_or_object, $method_name )" Returns a number of calling of \f(CW$method_name\fR in \f(CW$class_name_or_object\fR. .SH AUTHOR .IX Header "AUTHOR" Toru Yamaguchi .PP Yuji Shimada .PP Masaki Nakagawa .SH "THANKS TO" .IX Header "THANKS TO" cho45 .SH "SEE ALSO" .IX Header "SEE ALSO" Test::MockObject .SH LICENSE .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.