Thoughts about an easy-to-use Mocker for Perl

Unit-testing is the way to go in modern programing. When you’re at writing unit tests, chances are that you will eventually need the so-called mock objects.

You can find a pretty good definition of Mock objects here. In Perl, writing Mocks can be done with Test::MockClass and Test::MockObject which are pretty complete modules for that purpose.

However, I’ve came to gugod’s blog and found out his post about Mocking in Perl. I like his post and find his conclusion pertinent:

However, I still want my test program reads as simple as the rspec statement, it reads better. The most important of all, you don’t really need to know the whole pros and cons and the concept of “Mocking” to do it.

Hence I’ve been thinking at a module who could be named Test::Mocker – or even Test::EasyMocker – and who will be usable like the following.

use Test::Mocker 'Player';

Player->should( run => 42);

We could also imagine a more complex mock with a sub instead of a constant value:

use Test::Mocker 'Player';

Player->should( run => sub { return 42; });

As you can see, when importing Test::Mocker, we give it a list of packages we intend to mock in the test, that provides them with the “should” static method.

I like the ease of use of that syntax and find it pretty natural to write; I’d be happy to read your comments on this.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes