Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Test::Pockito, a new module for mocking stuff

by exussum0 (Vicar)
on Jul 26, 2010 at 20:02 UTC ( #851420=perlmeditation: print w/ replies, xml ) Need Help??

Hi. If you know java, do unit testing and have heard of Mockito, I've semi-ported it to perl.

Test::Pockito

I wanted to test Voldemort in a specific way and didn't find anything that supported testing in the way I wanted to, in the form of:

when something occurs, then respond however I want to, either in a default fashion, some code gets executed or return something.

Thanks, Corion, for being mildly excited about it, and providing some input. Lots of work left to do on it.

Edit: I noted Corion approved this node. If mentioning him makes him approve, I wonder if complimenting his dashing good looks will front page it. I keed.

Edit2: Well played ww

Comment on Test::Pockito, a new module for mocking stuff
Re: Test::Pockito, a new module for mocking stuff
by exussum0 (Vicar) on Jul 27, 2010 at 03:27 UTC
    Stolen from the readme
    package Love; sub new { return bless {}, Love; } sub marry { my $self = shift; my $user1 = shift; my $user2 = shift; my $db_object = $self->{'db_object'}; if ( $db_object->is_married($user1) == 0 && $db_object->is_married($user2) == 0 ) { $db_object->marry( $user1, $user2 ); $db_object->marry( $user2, $user1 ); return 1; } return 0; } package MyDbClass; sub is_married { # do some complicated stuff } sub marry { # do some other complicated stuff } #Our test can be use Test::Pockito; use Test::Simple; my $pocket = Test::Pockito->new("MyNamespace"); my $db_mock = $pocket->mock("MyDbClass"); $pocket->when( $db_mock->is_married("bob") )->then(0); $pocket->when( $db_mock->is_married("alice") )->then(0); $pocket->when( $db_mock->marry( "alice", "bob" ) )->then(); $pocket->when( $db_mock->marry( "bob", "alice" ) )->then(); my $target = Love->new(); $target->{'db_object'} = $db_mock; ok( $target->marry( "bob", "alice" ) == 1, "single to married == success!" ); ok( scalar keys %{ $pocket->expected_calls } == 0, "No extra cruft calls, huzzah!" );
    An easy improvement would be to have a count_expected_calls_outstanding or something silly, but it's a first release. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://851420]
Approved by Corion
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2015-07-02 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls