Test2::Mock - Module for managing mocked classes and instances.

NAME

DESCRIPTION

This module lets you add and override methods for any package temporarily. When the instance is destroyed it will restore the package to its original state.

SYNOPSIS

useTest2::Mock;useMyClass;my$mock=Test2::Mock->new(class=>'MyClass',override=>[name=>sub{'fred'},...],add=>[is_mocked=>sub{1}...],...);# Unmock the 'name' sub$mock->restore('name');...$mock=undef;# Will remove all the mocking

CONSTRUCTION

METHODS

$mock = Test2::Mock->new(class => $CLASS, ...)

This will create a new instance of Test2::Mock that manages mocking for the specified $CLASS.

Any Test2::Mock method can be used as a constructor argument, each should be followed by an arrayref of arguments to be used within the method. For instance the add() method:

add() and override() are the primary ways to add/modify methods for a class. Both accept the exact same type of arguments. The difference is that override will fail unless the symbol you are overriding already exists, add on the other hand will fail if the symbol does already exist.

Note: Think of override as a push operation. If you call override on the same symbol multiple times it will track that. You can use restore() as a pop operation to go back to the previous mock. reset can be used to remove all the mocking for a symbol.

Arguments must be a symbol name, with optional sigil, followed by a new specification of the symbol. If no sigil is specified then '&' (sub) is assumed. A simple example of overriding a sub:

$mock->override(foo=>sub{'overridden foo'});my$val=$class->foo;# Runs our override# $val is now set to 'overridden foo'

You can also simply provide a value and it will be wrapped in a sub for you:

$mock->override(foo=>'foo');

The example above will generate a sub that always returns the string 'foo'.

There are three *special* values that can be used to generate accessors:

$mock->add(name=>'rw',# Generates a read/write accessorage=>'ro',# Generates a read only accessorsize=>'wo',# Generates a write only accessor);

If you want to have a sub that actually returns one of the three special strings, or that returns a coderef, you can use a hashref as the spec:

You can also tell mock to deduce the symbol type for the add/override from the reference, rules are similar to glob assignments:

$mock->add(-foo=>sub{'foo'},# Adds the &foo sub to the package-foo=>{foo=>1},# Adds the %foo hash to the package-foo=>['f','o','o'],# Adds the @foo array to the package-foo=>\"foo",# Adds the $foo scalar to the package);

$mock->restore($SYMBOL)

Restore the symbol to what it was before the last override. If the symbol was recently added this will remove it. If the symbol has been overridden multiple times this will ONLY restore it to the previous state. Think of override as a push operation, and restore as the pop operation.

$mock->reset($SYMBOL)

Remove all mocking of the symbol and restore the original symbol. If the symbol was initially added then it will be completely removed.

$mock->orig($SYMBOL)

This will return the original symbol, before any mocking. For symbols that were added this will return undef.

$mock->current($SYMBOL)

This will return the current symbol.

$mock->reset_all

Remove all added symbols, and restore all overridden symbols to their originals.

$mock->add_constructor($NAME => $TYPE)

$mock->override_constructor($NAME => $TYPE)

This can be used to inject constructors. The first argument should be the name of the constructor. The second argument specifies the constructor type.

The hash type is the most common, all arguments are used to create a new hash that is blessed.

hash=>sub{my($class,%params)=@_;returnbless\%params,$class;};

The array type is similar to the hash type, but accepts a list instead of key/value pairs:

array=>sub{my($class,@params)=@_;returnbless\@params,$class;};

The ref type takes a reference and blesses it. This will modify your original input argument.

ref=>sub{my($class,$params)=@_;returnbless$params,$class;};

The ref_copy type will copy your reference and bless the copy:

ref_copy=>sub{my($class,$params)=@_;my$type=reftype($params);returnbless{%$params},$classif$typeeq'HASH';returnbless[@$params],$classif$typeeq'ARRAY';croak"Not sure how to construct an '$class' from '$params'";};

$mock->before($NAME, sub { ... })

This will replace the original sub $NAME with a new sub that calls your custom code just before calling the original method. The return from your custom sub is ignored. Your sub and the original both get the unmodified arguments.

$mock->after($NAME, sub { ... })

This is similar to before, except your callback runs after the original code. The return from your callback is ignored.