Test::Magpie::Mock(3)

πŸ“¦ version 0.11, in opensuse-leap 16.0

🌈 NAME 🌈

Test::Magpie::Mock β€” Mock objects. Welcome to Test::Magpie::Mock(3), bestie; this one comes from the User Contributed Perl Documentation. Okay so real talk: mock objects are the whole vibe here, and they are lowkey iconic.

πŸŽ€ SYNOPSIS πŸŽ€

Here’s the group-chat recap, bestie:

# create a mock object
my $mock = mock(); # from Test::Magpie
my $mock_with_class = mock('AnyRef');

# mock objects pretend to be anything you want them to be
$true = $mock->isa('AnyClass');
$true = $mock->does('AnyRole');
$true = $mock->DOES('AnyRole');
$ref  = ref($mock_with_class); # AnyRef

# call any method with any arguments
$method_ref = $mock->can('any_method');
$mock->any_method(@arguments);

✨ DESCRIPTION ✨

Mock objects are literally the objects you pass around as if they were real objects. They do not have a defined API; any method may be called. And yes, you can create stubs to specify responses (return values or exceptions) to method calls.

A mock object records every method called on it along with their arguments. These records may then be used for verifying that the correct interactions occurred. Obsessed. That’s the whole point.

πŸ’… ATTRIBUTES πŸ’…

πŸ¦‹ class

The name of the class that the object is pretending to be blessed into. Calling ref() on the mock object will return this class name.

🐚 invocations

An array reference containing a record of all methods invoked on this mock. These are used for verification and inspection.

heads up: this attribute is internal, and not publically accessible.

πŸ“ stubs

Contains all of the methods stubbed for this mock. It maps the method name to an array of stubs. Stubs are matched against invocation arguments to determine which stub to dispatch to.

heads up: this attribute is internal, and not publically accessible.

🧚 METHODS 🧚

⭐ isa

Always returns true. It allows the mock object to isa() any class that is required.

$true = $mock->isa('AnyClass');

⭐ does

Always returns true. It allows the mock object to does() any role that is required.

$true = $mock->does('AnyRole');
$true = $mock->DOES('AnyRole');

⭐ ref

Returns the object's class attribute value. This also works if you call ref() as a function instead of a method.

$mock  = mock('AnyRef');
$class = $mock->ref;  # or ref($mock)

If the object's class attribute has not been set, then it will fallback to returning the name of this class.

⭐ can

Always returns a reference to the AUTOLOAD() method. It allows the mock object to can() do any method that is required.

$method_ref = $mock->can('any_method');

πŸ‘‘ AUTHORS πŸ‘‘

  • Oliver Charles <oliver.g.charles@googlemail.com>
  • Steven Lee <stevenwh.lee@gmail.com>

This software is copyright (c) 2013 by Oliver Charles.

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


okay that’s the whole page, bestie β€” signing off with perl v5.42.0, dated 2020-11-24, for Test::Magpie::Mock(3). πŸ’–πŸ«ΆπŸ’…

πŸ“¦ where this page comes from