Unit Testing with Object Mothers

Unit tests are exciting enough (or not!)

We don’t need long and convoluted test code to obscure the features we’re actually testing.  So instead of newing up objects directly in our tests, we can use separate code called Object Mothers.

Martin Fowler (https://martinfowler.com/bliki/ObjectMother.html) explains that Object Mothers are nothing but canned objects for unit testing.

It’s so simple, you’re probably already doing it!  So pat yourself on the back and say to yourself: “I just implemented the Object Mother Pattern”.

Now you can group your Object Mothers by scenario in an intuitive tree.

Here’s an example.

And here’s a very readable test, using the canned object.

Now that you know what Object Mothers are, go ahead and refactor some of your tests.

Happy testing!