.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test::MockObject::Extends 3" .TH Test::MockObject::Extends 3 "2022-06-02" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::MockObject::Extends \- mock part of an object or class .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& 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( \*(Aqparent_method\*(Aq ) \& \->set_always( \-grandparent_method => 1 ) \& \->clear(); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" 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. .SH "METHODS" .IX Header "METHODS" .ie n .IP """new( $object | $class )""" 4 .el .IP "\f(CWnew( $object | $class )\fR" 4 .IX Item "new( $object | $class )" \&\f(CW\*(C`new()\*(C'\fR 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. \fB\s-1NOTE:\s0\fR this will modify the object in place. .Sp 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. .Sp If you've not yet loaded the class, this method will try to load it for you. This may fail, so beware. .Sp If you pass no arguments, it will assume you really meant to create a normal \&\f(CW\*(C`Test::MockObject\*(C'\fR object and will oblige you. .Sp Note that if you pass a class, the object returned will appear to be an instance of that class; \fIthis does not mock the class itself\fR. .ie n .IP """mock( $methodname, $sub_ref )""" 4 .el .IP "\f(CWmock( $methodname, $sub_ref )\fR" 4 .IX Item "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. .ie n .IP """unmock( $methodname )""" 4 .el .IP "\f(CWunmock( $methodname )\fR" 4 .IX Item "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, you can chain it. .ie n .IP """isa( $class )""" 4 .el .IP "\f(CWisa( $class )\fR" 4 .IX Item "isa( $class )" As you'd expect from a mocked object, this will return true for the class it's mocking. .SH "INTERNAL METHODS" .IX Header "INTERNAL METHODS" To do its magic, this module uses several internal methods: .IP "\(bu" 4 \&\f(CW\*(C`check_class_loaded( $parent_class )\*(C'\fR .Sp This verifies that you have the mockee defined. If not, it attempts to load the corresponding module for you. .IP "\(bu" 4 \&\f(CW\*(C`gen_autoload( $extended )\*(C'\fR .Sp Returns an \s-1AUTOLOAD\s0 subroutine for the mock object that checks that the extended object (or class) can perform the requested method, that Test::MockObject can perform it, or that the parent has an appropriate \&\s-1AUTOLOAD\s0 of its own. (It should have its own \f(CW\*(C`can()\*(C'\fR in that case too though.) .IP "\(bu" 4 \&\f(CW\*(C`gen_can( $extended )\*(C'\fR .Sp Returns a \f(CW\*(C`can()\*(C'\fR method for the mock object that respects the same execution order as \f(CW\*(C`gen_autoload()\*(C'\fR. .IP "\(bu" 4 \&\f(CW\*(C`gen_isa( $extended )\*(C'\fR .Sp Returns an \f(CW\*(C`isa()\*(C'\fR method for the mock object that claims to be the \&\f(CW$extended\fR object appropriately. .IP "\(bu" 4 \&\f(CW\*(C`gen_get_parents( $extended )\*(C'\fR .Sp Returns a \f(CW\*(C`_\|_get_parents()\*(C'\fR method for the mock object that claims to be the \&\f(CW$extended\fR object appropriately. .IP "\(bu" 4 \&\f(CW\*(C`gen_package( $extended )\*(C'\fR .Sp Creates a new unique package for the mock object with the appropriate methods already installed. .IP "\(bu" 4 \&\f(CW\*(C`get_class( $invocant )\*(C'\fR .Sp Returns the class name of the invocant, whether it's an object or a class name. .SH "CAVEATS" .IX Header "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 \f(CW\*(C`can()\*(C'\fR appropriately. .PP There are also potential name collisions with methods in this module or \&\f(CW\*(C`Test::MockObject\*(C'\fR, though this should be rare. .SH "AUTHOR" .IX Header "AUTHOR" chromatic, .PP Documentation bug fixed by Stevan Little. Additional \s-1AUTOLOAD\s0 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! .SH "BUGS" .IX Header "BUGS" No known bugs. .SH "COPYRIGHT" .IX Header "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