Test::EasyMock - A mock library which is usable easily.
use Test::EasyMock qw( create_mock expect replay verify reset ); my $mock = create_mock(); expect($mock->foo(1))->and_scalar_return('a'); expect($mock->foo(2))->and_scalar_return('b'); replay($mock); $mock->foo(1); # return 'a' $mock->foo(2); # return 'b' $mock->foo(3); # Unexpected method call.(A test is failed) verify($mock); # verify all expectations is invoked. reset($mock); expect($mock->foo(1, 2)->and_array_return('a', 'b'); expect($mock->foo({ value => 3 })->and_array_return('c'); replay($mock); $mock->foo(1, 2); # return ('a', 'b') $mock->foo({ value => 3 }); # return ('c') verify($mock); reset($mock); expect($mock->foo(1))->and_scalar_return('a'); expect($mock->foo(1))->and_scalar_return('b'); replay($mock); $mock->foo(1); # return 'a' $mock->foo(1); # return 'b' $mock->foo(1); # Unexpected method call.(A test is failed) verify($mock);
Using Test::Deep's special comparisons.
Test::Deep
use Test::EasyMock qw( create_mock expect replay verify reset whole ); use Test::Deep qw( ignore ); my $mock = create_mock(); expect($mock->foo(1, ignore())->and_scalar_return('a'); expect($mock->foo({ value => 1, random => ignore() })->and_scalar_return('b'); replay($mock); $mock->foo(1, 1234); # return 'a' $mock->foo({ value => 1, random => 1234 }); # return 'b' verify($mock); reset($mock); expect($mock->foo(whole(ignore())))->and_stub_scalar_return('a'); replay($mock); $mock->foo(); # return 'a' $mock->foo(1, 2, 3); # return 'a' $mock->foo({ arg1 => 1, arg2 => 2 }); # return 'a' verify($mock);
Mock to class method.
use Test::EasyMock qw( expect replay verify ); use Test::EasyMock::Class qw( create_class_mock ); my $mock = create_class_mock('Foo::Bar'); expect($mock->foo(1))->and_scalar_return('a'); replay($mock); Foo::Bar->foo(1); # return 'a' Foo::Bar->foo(2); # Unexpected method call.(A test is failed) verify($mock); # verify all expectations is invoked.
This is mock library modeled on 'EasyMock' in Java.
Creates a mock object. If specified the $module_name then a isa($module_name) method of the mock object returns true.
Record a method invocation and behavior.
The following example is expecting the foo method invocation with $arguments and a result of the invocation is 123.
expect($mock->foo($arguments)) ->and_scalar_return(123);
And the next example is expecting the foo method invocation without an argument and a result of the invocation is (1, 2, 3).
expect($mock->foo()) ->and_array_return(1, 2, 3);
Add scalar result to the expectation.
Add array result to the expectation.
Add list result to the expectation.
Add code to the expectation, it calculate an answer.
Add die behavior to the expectation.
Set scalar result as a stub to the expectation.
Set array result as a stub to the expectation.
Set list result as a stub to the expectation.
Add code as a stub to the expectation, it calculate an answer.
Set die behavior as as stub to the expectation.
Replay the mock object behaviors which is recorded by the expect function.
replay($mock);
Verify the mock method invocations.
Reset the mock.
It is a kind of an argument matcher. The matcher considers that the whole argument is array ref.
# same as `expect($mock->foo(1, 2))` expect($mock->foo( whole([1, 2]) )); # matches any arguments. (eg. foo(), foo(1,2), foo({}), etc...) expect($mock->foo( whole(ignore()) ));
keita iseki <keita.iseki+cpan at gmail.com>
<keita.iseki+cpan at gmail.com>
Copyright (c) 2012, keita iseki <keita.iseki+cpan at gmail.com>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
http://easymock.org/
It is a very wonderful library for the Java of a mock object.
http://search.cpan.org/~rjbs/Test-Deep-0.110/lib/Test/Deep.pm
http://search.cpan.org/~kiseki/Test-EasyMock/lib/Test/EasyMock/Class.pm
To install Test::EasyMock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::EasyMock
CPAN shell
perl -MCPAN -e shell install Test::EasyMock
For more information on module installation, please visit the detailed CPAN module installation guide.