Lire::Test::Mock(3) Create mock object

SYNOPSIS


use Lire::Report;
use Lire::Test::Mock;
use Lire::DlfResult;
my $mock = new Lire::Test::Mock( 'Lire::Report' );
$mock->set_result( 'timestamp', $time );
$mock->timestamp(); # will return $time
$mock->get_calls(); # [ 'timestamp' ]
$mock->get_invocation( 'timestamp', 0 ); # [ $mock ]

DESCRIPTION

This class makes it easy to defined mock objects. Mock objects are objects which offers the same interface than another object but which do not share its functionality. This makes it easier to test objects which requires fixtures which have lots of dependencies.

The mock object can be used to collect information about calls made on the object. Returns value for such method invocation can also be specified.

new( $class, 'method' => $result, 'method' => $result )

Creates a new mock object that will wrap $class. Any other keyword arguments will be use to initialize the result of methods call. See set_result() for information on how this works.

new_proxy( $class, @constructor_params )

This creates mock object which for the base $class. A proxy mock object will still monitor calls to the object but the real methods will be invoked, unless a result was specified using set_result(). Any remaining parameters will be passed to the new() method which should be defined in the class.

new_proxy( $instance )

Makes a Lire::Test::Mock object which is a clone of $instance.

is_proxy()

Returns whether this mock object will proxy to the real methods when no results was defined for a specific method.

get_calls()

Returns an array reference containing all the methods called on the object.

invocation_count( $method )

Returns the number of time $method was called.

get_invocation( $method, $index )

Returns the parameter that were given when method $method was called.

set_result( method => $result, ... )

This assign the result $result to $method. If $result is a code reference, it will be invoked with the same argument than the method to compute the result.

USING MOCK FACTORIES

Sometime, it is not possible to instatiate a proxy or mock object during fixture setup. This will usually happen when the object which we want to track access to is instantiated by the method under test. In these cases, one can use the set_mock_factory() class method to change the factory method to one that will return a proxy instance instead of a real instance. One should call reset_factories() during tear_down() so that the real factory method become directly accessible once again.

set_mock_factory( $class, %results )

Make the new() method of package $class returns proxy Lire::Test::Mock instance. The created instances will be accessible through the mock_instances() method. Any other argument will be passed to the set_result() method when the mock instance is created.

mock_instances( $class )

Returns an array reference containing all the instance that were created by the installed mock factory in $class. This method will throw an exception if now mock factory was installed for class $class.

reset_factories()

Removes all mock factories that were set up using set_mock_factory().

VERSION

$Id: Mock.pm,v 1.5 2006/07/23 13:16:32 vanbaal Exp $

AUTHORS

Francis J. Lacoste <[email protected]>

COPYRIGHT

Copyright (C) 2004 Stichting LogReport Foundation [email protected]

This file is part of Lire.

Lire is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program (see COPYING); if not, check with http://www.gnu.org/copyleft/gpl.html.