Test::M...:Extends(3)
π NAME π
okay bestie, welcome to the Test::MockObject::Extends manual page β mock part of an object or class. It lives under User Contributed Perl Documentation, and the header is giving truncated Test::M...:Extends(3) energy because terminal aesthetics are like that sometimes π
.
β¨ SYNOPSIS β¨
Here's the whole vibe in code:
use Some::Class;
use Test::MockObject::Extends;
# create an object to mock
my $object = Some::Class->new();
# wrap that same object with a mocking wrapper
$object = Test::MockObject::Extends->new( $object );
# now chain mock and control calls
$object->set_true( 'parent_method' )
->set_always( -grandparent_method => 1 )
->clear();
π¦ DESCRIPTION π¦
Test::MockObject::Extends lets you mock one or more methods of an existing object or class. This is lowkey iconic when you're testing a well-factored module that does almost exactly what you want. Wouldn't it be handy to take control of a method or two so you can make sure you receive testable results? Now you can, bestie. β¨
π METHODS π
π«Ά new( $object | $class )
new() takes one optional argument: the object or class to mock. If you're mocking a method for an object that holds internal state, create an appropriate object first, then pass it to this constructor.
β οΈ NOTE: this will modify the object in place. fr fr, heads up.
If you're mocking an object that does not need state β like when there's no internal data, or you'll only be calling class methods, or you'll be mocking all of the access to internal data β you can pass in the name of the class to mock partially.
If you've not yet loaded the class, this method will try to load it for you. This may fail, so beware. π
If you pass no arguments, it will assume you really meant to create a normal Test::MockObject object and will oblige you.
Note that if you pass a class, the object returned will appear to be an instance of that class; this does not mock the class itself.
π mock( $methodname, $sub_ref )
See the documentation for Test::MockObject for all of the ways to mock methods and to retrieve method logging information. These methods return the invocant, so you can chain them. Chain them like your life depends on it. π«Ά
π§ unmock( $methodname )
Removes any active mocking of the named method. This means any calls to that method will hit the method of that name in the class being mocked, if it exists. This method returns the invocant, so you can chain it.
π isa( $class )
As you'd expect from a mocked object, this will return true for the class it's mocking. Slay.
π₯ INTERNAL METHODS π₯
ngl, to do its magic, this module uses several internal methods:
check_class_loaded( $parent_class )β This verifies that you have the mockee defined. If not, it attempts to load the corresponding module for you.gen_autoload( $extended )β Returns anAUTOLOADsubroutine for the mock object that checks that the extended object (or class) can perform the requested method, thatTest::MockObjectcan perform it, or that the parent has an appropriateAUTOLOADof its own. (It should have its owncan()in that case too though.)gen_can( $extended )β Returns acan()method for the mock object that respects the same execution order asgen_autoload().gen_isa( $extended )β Returns anisa()method for the mock object that claims to be the$extendedobject appropriately.gen_get_parents( $extended )β Returns a__get_parents()method for the mock object that claims to be the$extendedobject appropriately.gen_package( $extended )β Creates a new unique package for the mock object with the appropriate methods already installed.get_class( $invocant )β Returns the class name of the invocant, whether it's an object or a class name.
π CAVEATS π
There may be some weird corner cases with dynamically generated methods in the mocked class. You really should use subroutine declarations though, or at least set can() appropriately. Don't make it weird.
There are also potential name collisions with methods in this module or Test::MockObject, though this should be rare.
πΈ AUTHOR πΈ
chromatic, <chromatic at wgz dot org>
Documentation bug fixed by Stevan Little. Additional AUTOLOAD approach suggested by Adam Kennedy. Field-based objects supported by Gavin Mogan. Other bugs reported by Paul the Nomad and Praveen Ray. Thank you all! π
π BUGS π
No known bugs. We love a clean vibe check. β¨
β COPYRIGHT β
Copyright (c) 2004 - 2014, chromatic. All rights reserved. You may use, modify, and distribute this module under the same terms as Perl 5.10
okay bestie, that's the whole page π
β this one is from source/version perl v5.42.0, dated 2020-01-23, and the footer's little title bit is Test::M...:Extends(3). bye bestie, go mock something cute ππ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-MockObject 1.20200122-bp160.1.12 Β· Artistic-1.0 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