Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

test::mockmodule(3pm) [debian man page]

Test::MockModule(3pm)					User Contributed Perl Documentation				     Test::MockModule(3pm)

NAME
Test::MockModule - Override subroutines in a module for unit testing SYNOPSIS
use Module::Name; use Test::MockModule; { my $module = new Test::MockModule('Module::Name'); $module->mock('subroutine', sub { ... }); Module::Name::subroutine(@args); # mocked } Module::Name::subroutine(@args); # original subroutine DESCRIPTION
"Test::MockModule" lets you temporarily redefine subroutines in other packages for the purposes of unit testing. A "Test::MockModule" object is set up to mock subroutines for a given module. The object remembers the original subroutine so it can be easily restored. This happens automatically when all MockModule objects for the given module go out of scope, or when you "unmock()" the subroutine. METHODS
new($package[, %options]) Returns an object that will mock subroutines in the specified $package. If there is no $VERSION defined in $package, the module will be automatically loaded. You can override this behaviour by setting the "no_auto" option: my $mock = new Test::MockModule('Module::Name', no_auto => 1); get_package() Returns the target package name for the mocked subroutines is_mocked($subroutine) Returns a boolean value indicating whether or not the subroutine is currently mocked mock($subroutine => &coderef) Temporarily replaces one or more subroutines in the mocked module. A subroutine can be mocked with a code reference or a scalar. A scalar will be recast as a subroutine that returns the scalar. The following statements are equivalent: $module->mock(purge => 'purged'); $module->mock(purge => sub { return 'purged'}); $module->mock(updated => [localtime()]); $module->mock(updated => sub { return [localtime()]}); However, "undef" is a special case. If you mock a subroutine with "undef" it will install an empty subroutine $module->mock(purge => undef); $module->mock(purge => sub { }); rather than a subroutine that returns "undef": $module->mock(purge => sub { undef }); You can call "mock()" for the same subroutine many times, but when you call "unmock()", the original subroutine is restored (not the last mocked instance). original($subroutine) Returns the original (unmocked) subroutine unmock($subroutine [, ...]) Restores the original $subroutine. You can specify a list of subroutines to "unmock()" in one go. unmock_all() Restores all the subroutines in the package that were mocked. This is automatically called when all "Test::MockObject" objects for the given package go out of scope. SEE ALSO
Test::MockObject::Extends Sub::Override AUTHOR
Simon Flack <simonflk _AT_ cpan.org> COPYRIGHT
Copyright 2004 Simon Flack <simonflk _AT_ cpan.org>. All rights reserved You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file. perl v5.10.0 2005-03-24 Test::MockModule(3pm)

Check Out this Related Man Page

Test::MockObject::Extends(3pm)				User Contributed Perl Documentation			    Test::MockObject::Extends(3pm)

NAME
Test::MockObject::Extends - mock part of an object or class SYNOPSIS
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 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. 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, then pass it to this constructor. NOTE: this will modify the object in place. 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. 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. "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. "isa( $class )" As you'd expect from a mocked object, this will return true for the class it's mocking. INTERNAL METHODS
To do its magic, this module uses several internal methods: o "check_class_loaded( $parent_class )" This verifies that you have the mockee defined. If not, it attempts to load the corresponding module for you. o "gen_autoload( $extended )" Returns an AUTOLOAD 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 AUTOLOAD of its own. (It should have its own "can()" in that case too though.) o "gen_can( $extended )" Returns a "can()" method for the mock object that respects the same execution order as "gen_autoload()". o "gen_isa( $extended )" Returns an "isa()" method for the mock object that claims to be the $extended object appropriately. o "gen_get_parents( $extended )" Returns a "__get_parents()" method for the mock object that claims to be the $extended object appropriately. o "gen_package( $extended )" Creates a new unique package for the mock object with the appropriate methods already installed. o "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. 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. Other bugs reported by Paul the Nomad and Praveen Ray. Thank you all! BUGS
No known bugs. COPYRIGHT
Copyright (c) 2004 - 2011, chromatic. All rights reserved. You may use, modify, and distribute this module under the same terms as Perl 5.10 perl v5.14.2 2012-03-04 Test::MockObject::Extends(3pm)
Man Page