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.
You might want to see Java’s (the finest IMO) JMock, or Ruby’s Mocha (used in Yoolink’s unit tests, especially expects() and any_instance()) for inspiration about how to simply express powerful constraints without touching the tested code.
Well, I’m sure the Java world is full of great ideas regarding object-oriented programming, sincerly. But I’m not sure JMock (or even Java) would be the appropriate source of inspiration for writing a lightweight and easy-to-read syntax…
Wait, oh, no, is this a troll? :-)