.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Mock 3pm" .TH Mock 3pm 2025-09-28 "Lire 2.1.1" "LogReport's Lire 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 Lire::Test::Mock \- Create mock object .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& use Lire::Report; \& use Lire::Test::Mock; \& use Lire::DlfResult; \& \& my $mock = new Lire::Test::Mock( \*(AqLire::Report\*(Aq ); \& $mock\->set_result( \*(Aqtimestamp\*(Aq, $time ); \& $mock\->timestamp(); # will return $time \& $mock\->get_calls(); # [ \*(Aqtimestamp\*(Aq ] \& $mock\->get_invocation( \*(Aqtimestamp\*(Aq, 0 ); # [ $mock ] .Ve .SH DESCRIPTION .IX Header "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. .PP 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. .ie n .SS "new( $class, \*(Aqmethod\*(Aq => $result, \*(Aqmethod\*(Aq => $result )" .el .SS "new( \f(CW$class\fP, \*(Aqmethod\*(Aq => \f(CW$result\fP, \*(Aqmethod\*(Aq => \f(CW$result\fP )" .IX Subsection "new( $class, 'method' => $result, 'method' => $result )" Creates a new mock object that will wrap \f(CW$class\fR. Any other keyword arguments will be use to initialize the result of methods call. See \&\fBset_result()\fR for information on how this works. .ie n .SS "new_proxy( $class, @constructor_params )" .el .SS "new_proxy( \f(CW$class\fP, \f(CW@constructor_params\fP )" .IX Subsection "new_proxy( $class, @constructor_params )" This creates mock object which for the base \f(CW$class\fR. A proxy mock object will still monitor calls to the object but the real methods will be invoked, unless a result was specified using \fBset_result()\fR. Any remaining parameters will be passed to the \fBnew()\fR method which should be defined in the class. .ie n .SS "new_proxy( $instance )" .el .SS "new_proxy( \f(CW$instance\fP )" .IX Subsection "new_proxy( $instance )" Makes a Lire::Test::Mock object which is a clone of \f(CW$instance\fR. .SS \fBis_proxy()\fP .IX Subsection "is_proxy()" Returns whether this mock object will proxy to the real methods when no results was defined for a specific method. .SS \fBget_calls()\fP .IX Subsection "get_calls()" Returns an array reference containing all the methods called on the object. .ie n .SS "invocation_count( $method )" .el .SS "invocation_count( \f(CW$method\fP )" .IX Subsection "invocation_count( $method )" Returns the number of time \f(CW$method\fR was called. .ie n .SS "get_invocation( $method, $index )" .el .SS "get_invocation( \f(CW$method\fP, \f(CW$index\fP )" .IX Subsection "get_invocation( $method, $index )" Returns the parameter that were given when method \f(CW$method\fR was called. .ie n .SS "set_result( method => $result, ... )" .el .SS "set_result( method => \f(CW$result\fP, ... )" .IX Subsection "set_result( method => $result, ... )" This assign the result \f(CW$result\fR to \f(CW$method\fR. If \f(CW$result\fR is a code reference, it will be invoked with the same argument than the method to compute the result. .SH "USING MOCK FACTORIES" .IX Header "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 \fBset_mock_factory()\fR class method to change the factory method to one that will return a proxy instance instead of a real instance. One should call \fBreset_factories()\fR during \&\fBtear_down()\fR so that the real factory method become directly accessible once again. .ie n .SS "set_mock_factory( $class, %results )" .el .SS "set_mock_factory( \f(CW$class\fP, \f(CW%results\fP )" .IX Subsection "set_mock_factory( $class, %results )" Make the \fBnew()\fR method of package \f(CW$class\fR returns proxy Lire::Test::Mock instance. The created instances will be accessible through the \&\fBmock_instances()\fR method. Any other argument will be passed to the \&\fBset_result()\fR method when the mock instance is created. .ie n .SS "mock_instances( $class )" .el .SS "mock_instances( \f(CW$class\fP )" .IX Subsection "mock_instances( $class )" Returns an array reference containing all the instance that were created by the installed mock factory in \f(CW$class\fR. This method will throw an exception if now mock factory was installed for class \f(CW$class\fR. .SS \fBreset_factories()\fP .IX Subsection "reset_factories()" Removes all mock factories that were set up using \fBset_mock_factory()\fR. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBTest::Unit::TestCase\fR\|(3pm) .SH VERSION .IX Header "VERSION" \&\f(CW$Id:\fR Mock.pm,v 1.5 2006/07/23 13:16:32 vanbaal Exp $ .SH AUTHORS .IX Header "AUTHORS" Francis J. Lacoste .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright (C) 2004 Stichting LogReport Foundation LogReport@LogReport.org .PP This file is part of Lire. .PP 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. .PP 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. .PP 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.