Test::S...n::Mock(3)
β¨ 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$mockobject (Test::Stream::Mock) back. Vibe check complete. β$mock = mock $class => ( ... );$mock = mock $instance => ( ... )$mock = mock 'class', $class => ( ... )β These forms delegate tomock_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 tomock_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$mockas the current mock build, then runs the sub. Inside that 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 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 ofTest::Stream::Mockto 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$mockas the current build, then runs the specified code. Once the sub is done,$mockis no longer the current build. Bye bestie. π$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. 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 π
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
- fedora 43: perl-Test-Stream 1.302027-35.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms