Test::Mock::Class::Role::Object - Role for base object of mock class
This role provides an API for defining and changing behavior of mock class.
Count of method calls stored as HashRef.
Expectations for mock methods stored as HashRef.
Return values or actions for mock methods stored as HashRef.
Check the expectations at the end. It should be called expicitly if minimum or count parameter was used for expectation, or following methods was called: mock_expect_at_least_once, mock_add_expectation_call_count, mock_expect_minimum_call_count or mock_expect_once.
minimum
count
mock_expect_at_least_once
mock_add_expectation_call_count
mock_expect_minimum_call_count
mock_expect_once
Increases the call counter and returns the expected value for the method name and checks expectations. Will generate any test assertions as a result of expectations if there is a test present.
If more that one expectation matches, all of them are checked. If one of them fails, the whole mock_invoke method is failed.
mock_invoke
This method is called in overridden methods of mock class, but you need to call it explicitly if you constructed own method.
Sets a return for a parameter list that will be passed on by call to this method that match.
The first value is returned if more than one parameter list matches method's arguments. The undef value is returned if none of parameters matches.
undef
Method name.
Returned value.
$m->mock_return( 'open', 1 );
If value is coderef, then it is called with method name, current timing and original arguments as arguments. It allows to return array rather than scalar.
$m->mock_return( 'sequence', sub { qw( one two three )[ $_[1] ] } ); $m->mock_return( 'get_array', sub { (1,2,3) } );
Value is returned only for current timing, started from 0.
0
$m->mock_return( 'sequence', 'one', at => 0 ); $m->mock_return( 'sequence', 'two', at => 1 ); $m->mock_return( 'sequence', 'three', at => 2 );
Value is returned only if method is called with proper argument.
$m->mock_return( 'get_value', 'admin', args => ['dbuser'] ); $m->mock_return( 'get_value', 'secret', args => ['dbpass'] ); $m->mock_return( 'get_value', sub { $_[2] }, args => [qr/.*/] );
Convenience method for returning a value upon the method call.
Sets up a trigger to throw an exception upon the method call. The method takes the same arguments as mock_return.
mock_return
If an exception parameter is a string, the Exception::Assertion is thrown with this parameter as its message and rest of parameters as its arguments. If an exception parameter is an object reference, the throw method is called on this object with predefined message and rest of parameters as its arguments.
throw
Convenience method for throwing an error upon the method call.
Sets up an expected call with a set of expected parameters in that call. Each call will be compared to these expectations regardless of when the call is made. The method takes the same arguments as mock_return.
Sets up an expected call with a set of expected parameters in that call.
Sets an expectation for the number of times a method will be called. The mock_tally method have to be used to check this.
mock_tally
Sets the number of times a method may be called before a test failure is triggered.
Sets the number of times to call a method to prevent a failure on the tally.
Convenience method for barring a method call.
Convenience method for a single method call.
Convenience method for requiring a method call.
Finds the return value matching the incoming arguments. If there is no matching value found then an error is triggered.
Adds one to the call count of a method and returns previous value.
Tests the arguments against expectations.
Test::Mock::Class.
The expectations and return values should be refactored as objects rather than complex structure.
The API is not stable yet and can be changed in future.
Piotr Roszatycki <dexter@cpan.org>
Based on SimpleTest, an open source unit test framework for the PHP programming language, created by Marcus Baker, Jason Sweat, Travis Swicegood, Perrick Penet and Edward Z. Yang.
Copyright (c) 2009, 2010 Piotr Roszatycki <dexter@cpan.org>.
This program is free software; you can redistribute it and/or modify it under GNU Lesser General Public License.
To install Test::Mock::Class, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Mock::Class
CPAN shell
perl -MCPAN -e shell install Test::Mock::Class
For more information on module installation, please visit the detailed CPAN module installation guide.