Test2::Tools::Mock(3)
π
Test2::Tools::Mock β the mocking page (from the Test2::Tools::Mock(3) perl manual) π
okay so bestie, welcome to Test2::Tools::Mock β class/instance mocking for Test2, and honestly it's giving main character energy for your test suite β¨
πΈ DESCRIPTION πΈ
Mocking is lowkey an essential part of testing, fr fr. This library covers some of the most common mocking needs and it's heavily influenced by Mock::Quick β but with an improved API, periodt. It's also designed to play nice with other plugins in ways Mock::Quick literally couldn't. π«Ά
β¨ SYNOPSIS β¨
my $mock = mock 'Some::Class' => (
track => $BOOL, # Enable/Disable tracking on subs defined below
add => [
new_method => sub { ... },
],
override => [
replace_method => sub { ... },
],
set => [
replace_or_inject => sub { ... },
],
track => $bool, # enable/disable tracking again to affect mocks made after this point
..., # Argument keys may be repeated
);
Some::Class->new_method(); # Calls the newly injected method
Some::Class->replace_method(); # Calls our replacement method.
$mock->override(...) # Override some more
$mock = undef; # Undoes all the mocking, restoring all original methods.
my $simple_mock = mock {} => (
add => [
is_active => sub { ... }
]
);
$simple_mock->is_active(); # Calls our newly mocked method.
π EXPORTS π
π DEFAULT
mockβ okay so this is the one-stop shop function, and it delegates to one of the other methods depending on how you use it. If you're not comfy with a function that has a lot of potential behaviors (it's giving multitudes π), you can always use one of the other functions directly. no pressure bestie.@mocks = mocked($object)and@mocks = mocked($class)β check if an object or class is mocked π΅οΈββοΈ. If it is mocked, you get the$mockobject(s) (Test2::Mock) returned. slay.$mock = mock $class => ( ... );,$mock = mock $instance => ( ... ), and$mock = mock 'class', $class => ( ... )β these forms delegate tomock_class()to mock a package. That third form is for when you wanna be explicit about what type of mocking you want π$obj = mock(),$obj = mock { ... }, and$obj = mock 'obj', ...;β these forms delegate tomock_obj()to create instances of anonymous packages where methods are vivified into existence as needed β¨mock $mock => sub { ... }andmock $method => ( ... )β these two go together π«Ά. The first form sets$mockas the current mock build, then runs the sub. Inside the sub you can declare mock specifications using the second form. The first form delegates tomock_build().
The second form calls the specified method on the current build. This second form delegates to mock_do().
π¦ BY REQUEST
π DEFINING MOCKS
$obj = mock_obj( ... ),$obj = mock_obj { ... } => ( ... ),$obj = mock_obj sub { ... }, and$obj = mock_obj { ... } => sub { ... }β this method lets you quickly generate a blessed object π. The object will be an instance of a randomly generated package name. Methods vivify as read/write accessors as needed.
Arguments can be any method available to Test2::Mock followed by an argument. If the very first argument is a hashref then it will be blessed as your new object π
If you provide a coderef instead of key/value pairs, the coderef will be run to build the mock. (See the "BUILDING MOCKS" section.)
$mock = mock_class $class => ( ... ),$mock = mock_class $instance => ( ... ), and$mock = mock_class ... => sub { ... }β this creates a new instance ofTest2::Mockto control the package specified. If you give it a blessed reference it'll use the class of the instance.
Arguments can be any method available to Test2::Mock followed by an argument. If the very first argument is a hashref then it will be blessed as your new object.
If you provide a coderef instead of key/value pairs, the coderef will be run to build the mock. (See the "BUILDING MOCKS" section.)
π BUILDING MOCKS
mock_build $mock => sub { ... }β set$mockas the current build, then run the specified code.$mockwill no longer be the current build when the sub is complete. clean β¨$mock = mock_building()β get the current building$mockobject.mock_do $method => $argsβ run the specified method on the currently building object.
π§ METHOD GENERATORS
$sub = mock_accessor $fieldβ generate a read/write accessor for the specified field. This generates a sub like the following:
$sub = sub {
my $self = shift;
($self->{$field}) = @_ if @_;
return $self->{$field};
};
$sub = mock_getter $fieldβ generate a read only accessor for the specified field. This generates a sub like the following:
$sub = sub {
my $self = shift;
return $self->{$field};
};
$sub = mock_setter $fieldβ generate a write accessor for the specified field. This generates a sub like the following:
$sub = sub {
my $self = shift;
($self->{$field}) = @_;
};
%pairs = mock_accessors(qw/name1 name2 name3/)β generates several read/write accessors at once π₯. Returns key/value pairs where the key is the field name, and the value is the coderef.%pairs = mock_getters(qw/name1 name2 name3/)β generates several read only accessors at once. Returns key/value pairs where the key is the field name, and the value is the coderef.%pairs = mock_setters(qw/name1 name2 name3/)β generates several write accessors at once. Returns key/value pairs where the key is the field name, and the value is the coderef.
π MOCK CONTROL OBJECTS π
my $mock = mock(...);
Mock objects are instances of Test2::Mock β go check that page for their methods, iykyk π
π SOURCE π
The source code repository for Test2-Suite can be found at <https://github.com/Test-More/test-more/>. go star it bestie β
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
π¦ AUTHORS π¦
Chad Granum <exodist@cpan.org>
πΈ COPYRIGHT πΈ
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <https://dev.perl.org/licenses/>
okay and that's the whole page bestie, no cap π«Ά this all came from perl v5.42.0 on 2025-07-28 β Test2::Tools::Mock(3), iconic, we love to see it πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Simple 1.302214-bp160.1.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms