Part eleven of a series of posts about automated testing.

To make unit testing possible, you need to be able to mock out accesses to resources such as the filesystem and databases. The best way to make that possible is to use dependency injection - ask for the collaborators at construction time.

For example, in Perl:

package Santa::Workshop;

use Moo;

has elves => (is => 'ro', required => 1);
has conveyor_belt => (is => 'ro', required => 1);
has wrapping_paper => (is => 'ro', required => 1);

...

These collaborators can be wired up using a dependency injection framework such as Bread::Board, or manually. Mocking these collaborators out becomes much easier than if they were created by the Workshop class itself.

Further reading