DevIQ
Object Mother Pattern
Object Mother Pattern
The Object Mother pattern was described by Martin Fowler as a catchy name for a factory that returns standard fixtures that can be used across multiple tests. The name itself was coined on a Thoughtworks project.
The Object Mother pattern builds upon the Factory Method pattern. The Object Mother pattern adds methods to customize the creation of objects, update the objects during testing, and enable object deletion from data storage. Object creation is handled in the Object Mother class, rather than within the tests.
For example, an Object Mother class for users - such as in devBetter - could look something like this:
The downfall to the Object Mother pattern is that the class can become bloated over time with repetitive code. DRY is important to keep in mind, as there may be parts of object creation that are repeated between different types of objects with the same mother.
An alternative to the Object Mother pattern is the Builder pattern.
References
See Also
- Builder Pattern
- Factory Method Pattern
- Testing - Overview
- On-Demand Webinar: Exploring Design Patterns for Testing