ABOUT THIS TUTORIAL

Unit testing on object aggregations are usually hard to develop because you may need to instantiate several complex objects, so you may wind up doing integral testing in order to test some very specific subclass. Test::MockObject, written by chromatic was designed to aid with this situation, nevertheless some things may not be immediately apparent for the novice user. Also, if you are testing OO stuff that uses accessors (Class::Accessor, Moose, etc.) you may find it difficult to do so. This tutorial is targeted for example to Moose developers, who will frequently encounter accessors and isa validations (see Moose::Manual::Attributes).

THE METHOD

Test::MockObject allows you to create objects that mock certain specific functionality, and that don't depend on anything more than the Test::MockObject module. It also allows for you to cheat Perl into thinking that a package has already been loaded, preventing Perl from doing so in your test, and forcing it to use your mocked package instead. More so, you can also fiddle with the mocked object's isa so that your mocked objects may pass UNIVERSAL->isa validations in the target code. What Test::MockObject does not currently do, is provide a simple mechanism to emulate accessors.

Here is one method to do it, maybe not the best, but will do the job. The example is mocking an object that uses another object which is also mocked, showing off some of the the powers of Test::MockObject.