Test2:...::Mock(3perl)
π Test2::Tools::Mock β the Test2:...::Mock(3perl) manual, bestie edition β¨
okay so you wanna do some mocking in your tests? say less. this is Test2::Tools::Mock, and it lives in the Perl Programmers Reference Guide, and it is literally here to make your test suite iconic.
πΈ NAME πΈ
Test2::Tools::Mock β Class/Instance mocking for Test2. π
π¦ DESCRIPTION π¦
so, mocking? essential part of testing, no cap. this library covers some of the most common mocking needs you're gonna run into. it's heavily influenced</> by Mock::Quick, except the API got a glow-up. and it's also built to play nice with other plugins in ways that Mock::Quick would simply be unable to. it's giving teamwork, it's giving growth. π«Ά
β¨ SYNOPSIS β¨
here's the vibe, look:
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.
yes the argument keys may be repeated. yes that's intentional. periodt. π
π EXPORTS π
π DEFAULT π
mockβ this is the one-stop shop function, bestie. it delegates to one of the other methods depending on how you use it. if a function with a lot of potential behaviors makes you nervous, you can always just use one of the other functions directly. no judgment. π@mocks = mocked($object)@mocks = mocked($class)β check if an object or class is mocked. if it is mocked, the$mockobject(s) (Test2::Mock) will be returned to you. a lil vibe check for your packages. π$mock = mock $class => ( ... );$mock = mock $instance => ( ... )$mock = mock 'class', $class => ( ... )β these forms delegate tomock_class()to mock a package. the third form exists so you can be explicit about what type of mocking you want. main character energy: state your intentions. π$obj = mock()$obj = mock { ... }$obj = mock 'obj', ...;β these forms delegate tomock_obj()to create instances of anonymous packages, where methods are vivified into existence as needed. β¨ poof, methods.mock $mock => sub { ... }mock $method => ( ... )β these two go together, ok? π€ the first form will set$mockas the current mock build, then run the sub. within 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 { ... }$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 will vivify as read/write accessors as needed.
arguments can be any method available to Test2::Mock followed by an argument. and 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, it's right below, keep scrolling bestie).
$mock = mock_class $class => ( ... )$mock = mock_class $instance => ( ... )$mock = mock_class ... => sub { ... }β this creates a new instance ofTest2::Mockto control the package you specified. if you hand it a blessed reference, it'll just use the class of the instance. easy. β¨
arguments can be any method available to Test2::Mock followed by an argument. if the very first argument is a hashref, it will be blessed as your new object.
and if you provide a coderef instead of key/value pairs, the coderef runs to build the mock. (again: see BUILDING MOCKS).
π§ BUILDING MOCKS π§
mock_build $mock => sub { ... }β set$mockas the current build, then run the specified code.$mockwill no longer be the current build once the sub is complete. it had its moment. π¬$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. it'll generate a sub like this:
$sub = sub {
my $self = shift;
($self->{$field}) = @_ if @_;
return $self->{$field};
};
$sub = mock_getter $fieldβ generate a read only accessor for the specified field. generated sub looks like:
$sub = sub {
my $self = shift;
return $self->{$field};
};
$sub = mock_setter $fieldβ generate a write accessor for the specified field. generated sub looks like:
$sub = sub {
my $self = shift;
($self->{$field}) = @_;
};
%pairs = mock_accessors(qw/name1 name2 name3/)β generates several read/write accessors at once, and returns key/value pairs where the key is the field name and the value is the coderef. bulk slay. π%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 see that one 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 or whatever. β
πΈ 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/>
and that's the tea, bestie π this was Test2:...::Mock(3perl) from perl v5.44.0, dated 2026-10-17. no cap, go mock something responsibly. π
β¨π¦
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms