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 $mock object(s) (Test2::Mock) get returned. cute, right? πŸ₯°
  • $mock = mock $class => ( ... );
  • $mock = mock $instance => ( ... )
  • $mock = mock 'class', $class => ( ... ) β€” these forms delegate to mock_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 to mock_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 $mock as the current mock build, then runs the sub. Within the sub you can declare mock specifications using the second form. The first form delegates to mock_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 of Test2::Mock to 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 $mock as the current build, then runs the specified code. $mock will no longer be the current build when the sub is complete. clean exit, slay.
  • $mock = mock_building() β€” gets the current building $mock object. πŸ’«
  • 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 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