Test::Mock::Guard(3pm)User Contributed Perl DocumentatioTest::Mock::Guard(3pm)
NAME
Test::Mock::Guard - Simple mock test library using RAII.
SYNOPSIS
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( 'Some::Class', { 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;
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.
EXPORT FUNCTION
mock_guard( @class_defs )
@class_defs have the following format.
key Class name or object to mock.
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.
You can mock instance methods as well as class methods (this feature
was provided by cho45):
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;
METHODS
new( @class_defs )
See "mock_guard" definition.
call_count( $class_name_or_object, $method_name )
Returns a number of calling of $method_name in $class_name_or_object.
AUTHOR
Toru Yamaguchi <zigorou@cpan.org>
Yuji Shimada <xaicron at cpan.org>
Masaki Nakagawa <masaki@cpan.org>
THANKS TO
cho45 <cho45@lowreal.net>
SEE ALSO
Test::MockObject
LICENSE
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
perl v5.32.0 2021-01-04 Test::Mock::Guard(3pm)
Czas wygenerowania: 0.00016 sek.
Created with the man page lookup class by Andrew Collington.
Based on a C man page viewer by Vadim Pavlov
Unicode soft-hyphen fix (as used by RedHat) by Dan Edwards
Some optimisations by Eli Argon
Caching idea and code contribution by James Richardson
Copyright © 2003-2025 Linux.pl
Hosted by Hosting Linux.pl