man Lire::Test::Mock () - Create mock object

NAME

Lire::Test::Mock - 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. Creates a new mock object that will wrap CW$class. Any other keyword arguments will be use to initialize the result of methods call. See set_result() for information on how this works. This creates mock object which for the base CW$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. Makes a Lire::Test::Mock object which is a clone of CW$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. Returns the number of time CW$method was called. Returns the parameter that were given when method CW$method was called. This assign the result CW$result to CW$method. If CW$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. Make the new() method of package CW$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. Returns an array reference containing all the instance that were created by the installed mock factory in CW$class. This method will throw an exception if now mock factory was installed for class CW$class.

reset_factories()

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

SEE ALSO

VERSION

$Id: Mock.pm,v 1.4 2004/08/28 00:22:02 flacoste Exp $

AUTHORS

Francis J. Lacoste <flacoste@logreport.org>

COPYRIGHT

Copyright (C) 2004 Stichting LogReport Foundation LogReport@LogReport.org

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 or write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.