Showcasing Test::TinyMocker

For a while we've been using in Dancer a few mocking subroutines that we've put together quite elegantly as TinyMocker in our 't/lib' folder to assist us in tests.

However, with time we've been asked (okay, Sukria has been asked) to separate this so others could use it outside of Dancer. Sukria has released this as Test::TinyMocker.

Test::TinyMocker allows to mock subroutines (be it in object oriented or basic packaged subroutines). Here is how it will look:


use Test::More tests => 21;
use Test::TinyMocker;
use My::Object;
mock 'My::Object::run' => sub {
isa_ok( $_[0], 'My::Object' );
...
};
my $object = My::Object->new();
isa_ok( $object, 'My::Object' );
$object->run(); # will run some tests instead of the actual method

A nice aspect of TinyMocker is that it tries to also be expressive (which we also enjoy in Perl-world) and allow you to write in a way that is easier for people to read (albeit a bit longer for you to write):


mock 'My::Object'
=> method 'run'
=> should {
isa_ok( $_[0], 'My::Object' );
};

If you use an arrayref, it automatically understands you want to mock several methods at once. It also provides "methods" for readability:

mock 'My::Object'
=> methods [ 'run', 'walk' ]
=> should {
...
}

I hope this helps you as much as it helps us!

Leave a comment

About Sawyer X

user-pic Gots to do the bloggingz