Test::MockObject::Extends - mock part of an object or class
use Some::Class; use Test::MockObject::Extends;
my $object = Some::Class->new(); my $mock_object = Test::MockObject::Extends->new( $object );
$mock_object->set_true( 'parent_method' );
Test::MockObject::Extends lets you mock one or more methods of an existing object or class. This can be very handy 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 to make sure you receive testable results? Now you can.
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, then pass it to this constructor.
If you're mocking an object that does not need state, as in the cases where there's no internal data or you'll only be calling class methods, or where 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.
mock( $methodname, $sub_ref )
unmock( $methodname )
isa( $class )
To do its magic, this module uses several internal methods:
gen_autoload( $extended )
can()
in that case too
though.)
gen_can( $extended )
can()
method for the mock object that respects the same execution
order as gen_autoload()
.
gen_isa( $extended )
isa()
method for the mock object that claims to be the
$extended
object appropriately.
gen_package( $extended )
get_class( $invocant )
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.
There are also potential name collisions with methods in this module or
Test::MockObject
, though this should be rare.
chromatic, <chromatic at wgz dot org>
Documentation bug fixed by Stevan Little. Additional AUTOLOAD approach suggested by Adam Kennedy.
No known bugs.
Copyright (c) 2004 - 2005, chromatic. All rights reserved. You may use, modify, and distribute this module under the same terms as Perl 5.8.x.