GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Test::Mock::Guard(3) User Contributed Perl Documentation Test::Mock::Guard(3)

Test::Mock::Guard - Simple mock test library using RAII.

  use Test::More;
  use Test::Mock::Guard qw(mock_guard);

  package Some::Class;

  sub new { bless {} => shift }
  sub foo { "foo" }
  sub bar { 1; }

  package main;

  {
      my $guard = mock_guard( 'Some::Class', { foo => sub { "bar" }, bar => 10 } );
      my $obj = Some::Class->new;
      is( $obj->foo, "bar" );
      is( $obj->bar, 10 );
  }

  my $obj = Some::Class->new;
  is( $obj->foo, "foo" );
  is( $obj->bar, 1 );

  done_testing;

Test::Mock::Guard is mock test library using RAII. This module is able to change method behavior by each scope. See SYNOPSIS's sample code.

@class_defs have the following format.
key
Class name or object to mock.
value
Hash reference. Keys are method names; values are code references or scalars. If the value is code reference, it is used as a method. If the value is a scalar, the method will return the specified value.

You can mock instance methods as well as class methods (this feature was provided by cho45):

  use Test::More;
  use Test::Mock::Guard qw(mock_guard);

  package Some::Class;

  sub new { bless {} => shift }
  sub foo { "foo" }

  package main;

  my $obj1 = Some::Class->new;
  my $obj2 = Some::Class->new;

  {
      my $obj2 = Some::Class->new;
      my $guard = mock_guard( $obj2, { foo => sub { "bar" } } );
      is ($obj1->foo, "foo", "obj1 has not changed" );
      is( $obj2->foo, "bar", "obj2 is mocked" );
  }

  is( $obj1->foo, "foo", "obj1" );
  is( $obj2->foo, "foo", "obj2" );

  done_testing;

See "mock_guard" definition.

Returns a number of calling of $method_name in $class_name_or_object.

Toru Yamaguchi <zigorou@cpan.org>

Yuji Shimada <xaicron at cpan.org>

Masaki Nakagawa <masaki@cpan.org>

cho45 <cho45@lowreal.net>

Test::MockObject

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-04-09 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.