Mock(3pm)

πŸ“¦ version 2.1.1, in arch rolling

πŸ’… Mock(3pm) β€” Lire::Test::Mock

okay so bestie, this is Lire::Test::Mock, straight out of LogReport's Lire documentation, and it's literally the friend that shows up to the group chat pretending to be somebody else so you can test your code without dragging in their whole chaotic extended family. iconic behavior, honestly. 🫢


✨ 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 🌸

so this class makes it super easy to define mock objects. and what even is a mock object? okay so β€” it's an object that offers the exact same interface as another object but does not share its functionality. it's giving identical twin but the twin has none of the original's baggage. πŸ’…

the whole point: it makes testing way easier for objects that need fixtures with, like, a ton of dependencies. no cap, that's the vibe.

your mock object can also collect info about all the calls made on it, and you can specify what each method invocation returns. obsessed. ✨


πŸŽ€ new( $class, 'method' => $result, 'method' => $result )

creates a brand new mock object that wraps $class. any other keyword arguments get used to initialize the results of method calls. for the deets on how that works, see set_result(). 🌸


πŸ¦‹ new_proxy( $class, @constructor_params )

creates a mock object for the base $class. a proxy mock object will still monitor calls to the object, but the real methods get invoked β€” unless a result was specified using set_result(). any remaining parameters get passed to the new() method, which should be defined in the class. main character proxy energy. πŸ‘‘


🧚 new_proxy( $instance )

makes a Lire::Test::Mock object that's a clone of $instance. twin telepathy, but in Perl. πŸ’œ


⭐ is_proxy()

returns whether this mock object will proxy to the real methods when no result was defined for a specific method. simple vibe check. βœ…


πŸ“ get_calls()

returns an array reference containing all the methods called on the object. the receipts, basically. πŸ“‹


🌈 invocation_count( $method )

returns the number of times $method was called. count 'em. πŸ”’


πŸ’– get_invocation( $method, $index )

returns the parameters that were given when method $method was called. so you can see exactly what it was handed. πŸ™ˆ


πŸ’… set_result( method => $result, ... )

this assigns the result $result to $method. and if $result is a code reference, it gets invoked with the same arguments as the method, so it can compute the result on the fly. fancy, right? ✨


πŸ‘‘ USING MOCK FACTORIES πŸ‘‘

okay so sometimes it's just not possible to instantiate a proxy or mock object during fixture setup 😭. usually this happens when the object we want to track access to is instantiated by the method under test itself. rude, but okay.

in these cases, you can use the set_mock_factory() class method to swap the factory method for one that returns a proxy instance instead of a real instance. and you should call reset_factories() during tear_down() so that the real factory method becomes directly accessible once again. cleanup era. 🧹


πŸŽ€ set_mock_factory( $class, %results )

makes the new() method of package $class return a proxy Lire::Test::Mock instance. the created instances will be accessible through the mock_instances() method. any other argument gets passed to the set_result() method when the mock instance is created. 🫢


πŸ¦‹ mock_instances( $class )

returns an array reference containing all the instances that were created by the installed mock factory in $class. ⚠️ heads up: this method will throw an exception if no mock factory was installed for class $class. so like, install it first, bestie. πŸ’€


🌟 reset_factories()

removes all mock factories that were set up using set_mock_factory(). factory reset, but make it self-care. πŸ§–β€β™€οΈ


πŸ”₯ SEE ALSO πŸ”₯


⭐ VERSION ⭐

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

πŸ’œ AUTHORS πŸ’œ

  • Francis J. Lacoste <flacoste@logreport.org>

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. 😀 (fr fr, read the license, it's important)

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.


k that's the whole page, no notes 🌈 β€” this was Lire 2.1.1, dated 2025-09-28, and it's the <rainbow>Mock(3pm)</rainbow> page, so you know exactly where to find it again. mwah πŸ’‹πŸ’–

πŸ“¦ where this page comes from