• Stars
    star
    11
  • Rank 1,694,829 (Top 34 %)
  • Language
    Perl
  • License
    Other
  • Created over 13 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple mock test library using RAII

NAME

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

SYNOPSIS

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;

DESCRIPTION

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.

EXPORT FUNCTION

mock_guard( @class_defs )

@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;

METHODS

new( @class_defs )

See "mock_guard" definition.

call_count( $class_name_or_object, $method_name )

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

AUTHOR

Toru Yamaguchi [email protected]

Yuji Shimada

Masaki Nakagawa [email protected]

THANKS TO

cho45 [email protected]

SEE ALSO

Test::MockObject

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

More Repositories

1

perl-JSV

JSON Schema implementation for Perl
Perl
25
star
2

p5-test-fixture-dbi

Perl
19
star
3

apue

Advanced Programming in UNIX Environment
C
14
star
4

yokohama-pm-10

Presentation of Yokohama Perl Monger #10
Perl
13
star
5

p5-data-ruledfactory

Perl
11
star
6

p5-sql-abstract-plugin-insertmulti

bulk insert and update support for SQL::Abstract
Perl
9
star
7

perl-json-pointer

A JSON Pointer implementation for Perl
Perl
8
star
8

perl-json-patch

A Perl implementation of JSON Patch
Perl
8
star
9

unix_network_programming

for study
C
6
star
10

p5-data-opensocial

Data representation for OpenSocial RESTful API
Perl
6
star
11

p5-mapreduce-simple

Simple MapReduce Library
Perl
6
star
12

p5-mysql-partitioning

Utility for MySQL Partitioning
Perl
4
star
13

p5-http-mobilejp-useragent

create useragent for mobilejp
Perl
4
star
14

lex_yacc_howto

4
star
15

p5-sql-builder

Builde sql query by perl structure
Perl
4
star
16

p5-keyedmutex-memcached

Keyed mutex using memcached
Perl 6
4
star
17

p5-nagios-plugin-memcached

Nagios plugin for memcached
Perl
3
star
18

p5-mysql-sandbox-frontend

Perl
3
star
19

p5-test-requires-env

Running test or not by dependency environment
Perl
3
star
20

p5-loop-sustainable

Perl
3
star
21

p5-iterator-groupedrange

Perl
2
star
22

p5-module-install-testvars

Passing variables before running tests
Perl
2
star
23

p5-xri-resolution-lite

Discovery on given XRI
Perl
2
star
24

p5-dbix-profilemanager

Helps to fine control your DBI profiling without hassles
Perl
2
star
25

p5-mysql-sustainablequery

Perl
2
star
26

p5-app-batch-toolkit

batch script toolkit
Perl
1
star
27

dotfiles

my dot files
Shell
1
star
28

p5-list-compressedsorted

C
1
star
29

temporary

Perl
1
star
30

p5-fdpool

Perl
1
star
31

json-pointer-test-suites

A test suite of JSON Pointer
1
star