Test::S...n::Mock(3)

πŸ“¦
version 1.302027 Β· 2 versions
, in fedora 43

✨ NAME ✨

Test::Stream::Plugin::Mock β€” mocking classes and instances, but make it cute, for Test::Stream. πŸ’…

(also this page is filed under Test::S...n::Mock(3) in your man pages, bc the name got squished, iykyk 🫢)


πŸ’€ DEPRECATED πŸ’€

Okay bestie, sit down. This whole distribution is deprecated. It's been replaced by Test2, Test2::Suite, and Test2::Workflow.

If you need to migrate, there's a conversion guide over at Test::Stream::Manual::ToTest2. No cap, go read it. 🧚


🌸 DESCRIPTION 🌸

So like, mocking is literally an essential part of testing. This library handles some of the most common mocking needs you'll run into. This plugin is heavily influenced by Mock::Quick, but the API is better β€” and it's also designed to play nice with the other plugins in ways Mock::Quick just... couldn't. It's giving glow-up. ✨


πŸŽ€ SYNOPSIS πŸŽ€

my $mock = mock 'Some::Class' => (
    add => [
        new_method => sub { ... },
    ],
    override => [
        replace_method => sub { ... },
    ],
);

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.

πŸ¦‹ MOCKING + SPEC TESTING πŸ¦‹

This plugin plays nicely with Test::Stream::Plugin::Spec. Here's the tea: mocks get treated as a "before_each" if you use the mock functions without saving the returned object. The mock will also apply to any describe block it's defined in.

describe stuff => sub {
    # The mock specification
    mock 'My::Class' => (...);

    # Mock applies here, inside the describe block

    tests foo => sub {
        # Mock applies here inside any nested blocks, even though they run
        # later
    };
};

# Mock does not apply out here

πŸ‘‘ EXPORTS πŸ‘‘

πŸ’– DEFAULT πŸ’–

  • mock β€” the 1-stop shop function, bestie. 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, you can just call one of the other functions directly instead. No pressure. 🫢
  • $mock = mocked($object) $mock = mocked($class) β€” Checks if an object or a class is mocked. If it is mocked, you get the $mock object (Test::Stream::Mock) back. Vibe check complete. βœ…
  • $mock = mock $class => ( ... ); $mock = mock $instance => ( ... ) $mock = mock 'class', $class => ( ... ) β€” These forms delegate to mock_class() to mock a package. That third form is you being explicit about what type of mocking you want. Main character energy. πŸ‘‘
  • $obj = mock() $obj = mock { ... } $obj = mock 'obj', ...; β€” These forms delegate to mock_obj() to create instances of anonymous packages, where methods get vivified into existence as needed. ✨
  • mock $mock => sub { ... } mock $method => ( ... ) β€” These two go together, okay? The first form sets $mock as the current mock build, then runs the sub. Inside that 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. Methods will vivify as read/write accessors as needed.

Arguments can be any method available to Test::Stream::Mock, followed by an argument. If the very first argument is a hashref, then it gets 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 creates a new instance of Test::Stream::Mock to control the package specified. If you give it a blessed reference, it'll use the class of the instance instead. Sneaky. πŸ™ˆ

Arguments can be any method available to Test::Stream::Mock, followed by an argument. If the very first argument is a hashref, then it gets 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. Once the sub is done, $mock is no longer the current build. Bye bestie. πŸ‘‹
  • $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. It'll generate a sub like this:
$sub = sub {
    my $self = shift;
    ($self->{$field}) = @_ if @_;
    return $self->{$field};
};
  • $sub = mock_getter $field β€” Generates a read obly accessor for the specified field. (yes, "obly", that's what the original says, we don't gatekeep typos here πŸ’€) It'll generate a sub like this:
$sub = sub {
    my $self = shift;
    return $self->{$field};
};
  • $sub = mock_setter $field β€” Generates a write accessor for the specified field. It'll generate a sub like this:
$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 Test::Stream::Mock β€” go look at that page for their methods, iykyk. πŸ’œ


πŸ”₯ SOURCE πŸ”₯

The source code repository for Test::Stream lives at http://github.com/Test-More/Test-Stream/. Go star it or whatever. ⭐


πŸ‘‘ MAINTAINERS πŸ‘‘

Chad Granum <exodist@cpan.org>


🌸 AUTHORS 🌸

Chad Granum <exodist@cpan.org>


Copyright 2015 Chad Granum <exodist7@gmail.com>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/


and that's the whole page, bestie!! 🫢 this was perl v5.42.0, dated 2025-07-25, filed under Test::S...n::Mock(3). periodt. πŸ’…βœ¨
πŸ“¦ where this page comes from