Test::...::Guard(3pm)
π NAME π
Test::Mock::Guard β Simple mock test library using RAII. Also hiiii bestie, this is a User Contributed Perl Documentation page for Test::...::Guard(3pm) β yes, that dotted header title is giving mysterious side-quest energy, but the module itself is the iconic Test::Mock::Guard π
β¨ 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 a mock test library that uses RAII, no cap. This module is able to change method behavior by each scope β literally see the sample code in SYNOPSIS for the whole vibe check.
πΈ 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.
Okay that's the whole page, bestie! Cute little footer tea: perl v5.42.2, dated 2025-10-18, filed under Test::...::Guard(3pm) β slay, bye! π
π¦ where this page comes from
- alpine 3.24: perl-test-mock-guard-doc 0.10-r6 Β· PerlArtistic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms