TEST2::TOOLS::MOCK(3pm)
π Test2::Tools::Mock β mocking for Test2, but make it cute (not the manual page's official name, just vibes)
Okay bestie, so this is Test2::Tools::Mock, a Perl module that does class/instance mocking for Test2. Sit down, I gotchu. π«Ά
β¨ NAME β¨
Test2::Tools::Mock β it does class/instance mocking for Test2. Periodt. π
π¦ DESCRIPTION π¦
Okay so mocking is literally an 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 (iconic), but with an improved API. And it's also intended to play well with other plugins in ways Mock::Quick would be unable to β like, the glow-up is real. π
πΈ SYNOPSIS πΈ
Lowkey the whole vibe in one block:
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β this is the one-stop shop function π it delegates to one of the other methods depending on how you use it. If you're not comfortable with a function that has a lot of potential behaviors (fair, honestly), you can use one of the other functions directly.@mocks = mocked($object)@mocks = mocked($class)β checks if an object or class is mocked. If it is mocked, the$mockobject(s) (Test2::Mock) get returned. cute, right? π₯°$mock = mock $class => ( ... );$mock = mock $instance => ( ... )$mock = mock 'class', $class => ( ... )β these forms delegate tomock_class()to mock a package. The third form is there so you can be explicit about what type of mocking you want. iykyk π$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. love that for us β¨mock $mock => sub { ... }mock $method => ( ... )β these two go together, bestie. the first form sets$mockas the current mock build, then runs 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, and methods will vivify as read/write accessors as needed. so convenient.
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 => ( ... )$mock = mock_class ... => sub { ... }β this will create a new instance ofTest2::Mockto control the package specified. If you give it a blessed reference it will 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 { ... }β sets$mockas the current build, then runs the specified code.$mockwill no longer be the current build when the sub is complete. clean exit, slay.$mock = mock_building()β gets the current building$mockobject. π«mock_do $method => $argsβ runs the specified method on the currently building object.
π METHOD GENERATORS
$sub = mock_accessor $fieldβ generates a read/write accessor for the specified field. This will generate a sub like the following:
$sub = sub {
my $self = shift;
($self->{$field}) = @_ if @_;
return $self->{$field};
};
$sub = mock_getter $fieldβ generates a read only accessor for the specified field. This will generate a sub like the following:
$sub = sub {
my $self = shift;
return $self->{$field};
};
$sub = mock_setter $fieldβ generates a write accessor for the specified field. This will generate 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. batch mode, obsessed. π₯%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. See it for their methods. main character energy, honestly. β¨
π SOURCE π
The source code repository for Test2-Suite can be found at <https://github.com/Test-More/test-more/>.
π 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 whole page, bestie β no gatekeeping, no cap π this whole thing is from perl v5.42.3, dated 2026-11-03 (you're welcome, that's the version + date tea π), and yes it's officially Test2::Tools::Mock(3pm) β okay bye, go write some tests, you're gonna slay β¨π¦
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl 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