Test::MonkeyMock - Usable mock class
# Create a new mock object my $mock = Test::MonkeyMock->new; $mock->mock(foo => sub { 'bar' }); $mock->foo; # Mock method when number of arguments is even $mock->mock(foo => sub { }, when => sub { @_ == 2 }); # Mock method when it's called only the first time $mock->mock(foo => sub { }, frame => 0); # Mock existing object my $mock = Test::MonkeyMock->new(MyObject->new()); $mock->mock(foo => sub { 'bar' }); $mock->foo; # Check how many times the method was called my $count = $mock->mocked_called('foo'); # Check what arguments were passed on the first call my @args = $mock->mocked_call_args('foo'); # Check what arguments were passed on the second call my @args = $mock->mocked_call_args('foo', 1); # Get all the stack my $call_stack = $mock->mocked_call_stack('foo');
Why? I used and still use Test::MockObject and Test::MockObject::Extends a lot but sometimes it behaves very strangely introducing hard to find global bugs in the test code, which is very painful, since the test suite should have as least bugs as possible. Test::MonkeyMock is somewhat a subset of Test::MockObject but without side effects.
Test::MonkeyMock is also very strict. When mocking a new object:
throw when using mocked_called on unmocked method
mocked_called
throw when using mocked_call_args on unmocked method
mocked_call_args
When mocking an existing object:
throw when using mock on unknown method
mock
throw when using mocked_called on unknown method
throw when using mocked_call_args on unknown method
new
Creates new mock or extends an existing object.
Test::MonkeyMock->new; Test::MonkeyMock->new($object);
can($method)
Returns what a real can does.
can
mock($method, $code, %options)
Mocks method with a subroutine.
Options are conditions that are checked when dispatching a method. If the condition fails the next candidate is taken.
when
When is called with original @_. Thus you can check for specific parameteres.
@_
my $mock = Test::MonkeyMock->new(); $mock->mock(foo => sub { 'bar' }, when => sub { @_ == 2 }); $mock->mock(foo => sub { 'else' }); is $mock->foo(1), 'bar'; is $mock->foo, 'else';
frame
Checks how many times the mocked method was called.
my $mock = Test::MonkeyMock->new(); $mock->mock(foo => sub { 'bar' }, frame => 0); $mock->mock(foo => sub { 'qux' }, frame => 2); $mock->mock(foo => sub { 'else' }); is $mock->foo, 'bar'; is $mock->foo, 'else'; is $mock->foo, 'qux'; is $mock->foo, 'else';
mocked_call_args($method, $frame)
Returns the arguments during method call. With $frame you can access the call stack.
$frame
mocked_call_stack($method)
Returns the complete call stack of the method.
mocked_called($method)
Returns how many times the method was called.
mocked_return_args($method, $frame)
Returns the return value of the method. With $frame you can access the call stack.
mocked_return_stack($method)
Returns the complete return stack of the method.
Viacheslav Tykhanovskyi, viacheslav.t@gmail.com
viacheslav.t@gmail.com
Copyright (C) 2012-2014, Viacheslav Tykhanovskyi
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
To install Test::MonkeyMock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::MonkeyMock
CPAN shell
perl -MCPAN -e shell install Test::MonkeyMock
For more information on module installation, please visit the detailed CPAN module installation guide.