Test::Mock::Signature - base class for mock modules.
Simple method:
use Test::More plan => 1; use Test::Mock::Signature qw( any ); use CGI; my $mock = Your::Mock::Module->new('CGI'); $mock->method('param' => any)->callback( sub { 42 } ); my $request = CGI->new; ok($request->param('something'), 42, 'mocked');
Or more complex. Create module for mocking CGI:
package Your::Mock::Module; use strict; use warnings; require Test::Mock::Signature; our @ISA = qw(Test::Mock::Signature); our $CLASS = 'CGI'; sub init { my $mock = shift; $mock->method('new')->callback( sub { my $class = shift; return bless({}, $class); } ); }
Use it in tests:
use Test::More plan => 1; use Your::Mock::Module qw( any ); use CGI; my $mock = Your::Mock::Module->new; $mock->method('param' => any)->callback( sub { 42 } ); my $request = CGI->new; ok($request->param('something'), 42, 'mocked');
This module is a base class for your mock module with ability to set callbacks for defined signature of your method.
This method imports magic constant any from class Data::PatternCompare and does some magic behind the scene. Also it takes real class name from your our $CLASS variable.
any
our $CLASS
Default constructor. By default accepts $class_name which should be mocked. In case of inheritance, class name goes from our $CLASS variable. To simplify inheritance there are another method defined init() which will be called from constructor.
$class_name
init()
Empty method invoked from constructor new(). Can be overrided to define default mocked methods e.g.: constructors.
new()
This method does the actual mocking of methods e.g.:
my $mock = Your::Mock::Module->new; my $cgi = new CGI; $mock->method(param => 'get_param')->callback( sub { return 42; } ); print $cgi->param('get_param'); # 42 print $cgi->param('ANYTHING_ELSE'); # will give original CGI::param behavior
@params can contain object any exported by the mock module if needed for detailed reference please look to: Data::PatternCompare.
@params
Returns object of Test::Mock::Signature::Meta class.
Clear mocking behavior from method. Takes $method_name as a first parameter. Prototype is optional. If you put only method name it remove all mocks from this method. If you put prototype parameters it finds this signature and delete it. e.g.:
$method_name
$mock->clear(param => 'get_param'); # delete exact signature $mock->clear('param'); # delete all mocked signatures from method "param"
This method returns dispatcher object for the given $method_name. Currently it's exposed as public just in case. Used for internal use and don't have any real user examples.
On destroying object, mocked methods are getting to their original behavior.
cono <cono@cpan.org>
Copyright 2014 - cono
Artistic v2.0
Data::PatternCompare
To install Test::Mock::Signature, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Mock::Signature
CPAN shell
perl -MCPAN -e shell install Test::Mock::Signature
For more information on module installation, please visit the detailed CPAN module installation guide.