NAME
Test::Mock::Class::Role::Object - Role for base object of mock class
DESCRIPTION
This role provides an API for defining and changing behavior of mock class.
ATTRIBUTES
- _mock_call : HashRef
-
Count of method calls stored as HashRef.
- _mock_expectation : HashRef
-
Expectations for mock methods stored as HashRef.
- _mock_action : HashRef
-
Return values or actions for mock methods stored as HashRef.
METHODS
- mock_tally() : Self
-
Check the expectations at the end. It should be called expicitly if
minimum
orcount
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
ormock_expect_once
. - mock_invoke( method : Str, args : Array ) : Any
-
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.This method is called in overridden methods of mock class, but you need to call it explicitly if you constructed own method.
- mock_return( method : Str, value : Any, :at : Int, :args : ArrayRef[Any] ) : Self
-
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.- method
-
Method name.
- value
-
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) } );
- at
-
Value is returned only for current timing, started from
0
.$m->mock_return( 'sequence', 'one', at => 0 ); $m->mock_return( 'sequence', 'two', at => 1 ); $m->mock_return( 'sequence', 'three', at => 2 );
- args
-
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/.*/] );
- mock_return_at( at : Int, method : Str, value : Any, :args : ArrayRef[Any] ) : Self
-
Convenience method for returning a value upon the method call.
- mock_throw( method : Str, :at : Int, exception : Str|Object, :args : ArrayRef[Any], params : Hash ) : Self
-
Sets up a trigger to throw an exception upon the method call. The method takes the same arguments as
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. - mock_throw_at( at : Int, method : Str, exception : Str|Object, :args : ArrayRef[Any] ) : Self
-
Convenience method for throwing an error upon the method call.
- mock_expect( method : Str, :at : Int, :minimum : Int, :maximum : Int, :count : Int, :args : ArrayRef[Any] ) : Self
-
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
. - mock_expect_at( at : Int, method : Str, :args : ArrayRef[Any] ) : Self
-
Sets up an expected call with a set of expected parameters in that call.
- mock_expect_call_count( method : Str, count : Int, :args : ArrayRef[Any] ) : Self
-
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_expect_maximum_call_count( method : Str, count : Int, :args : ArrayRef[Any] ) : Self
-
Sets the number of times a method may be called before a test failure is triggered.
- mock_expect_minimum_call_count( method : Str, count : Int, :args : ArrayRef[Any] ) : Self
-
Sets the number of times to call a method to prevent a failure on the tally.
- mock_expect_never( method : Str, :args : ArrayRef[Any] ) : Self
-
Convenience method for barring a method call.
- mock_expect_once( method : Str, :args : ArrayRef[Any] ) : Self
-
Convenience method for a single method call.
- mock_expect_at_least_once( method : Str, :args : ArrayRef[Any] ) : Self
-
Convenience method for requiring a method call.
- _mock_emulate_call( method : Str, timing : Int, args : Array ) : Any
-
Finds the return value matching the incoming arguments. If there is no matching value found then an error is triggered.
- _mock_add_call( method : Str, args : Array ) : Int
-
Adds one to the call count of a method and returns previous value.
- _mock_check_expectations( method : Str, timing : Num, args : Array ) : Self
-
Tests the arguments against expectations.
SEE ALSO
BUGS
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.
AUTHOR
Piotr Roszatycki <dexter@cpan.org>
LICENSE
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.