Object Mother
I just read an interesting post written by Martin Fowler regarding test fixtures. Fowler is outstanding in naming things and transforming complex concepts into easy-to-understand materials.
In technical perspectives, he proposes an object called Object Mother which is responsible to give birth to test fixtures. In other words, it’s a kind of factory object capable of delivering pre-set complex test fixtures objects. For example, it’s possible that you need various kinds of accounts to test different scenarios and corner cases in the banking domain. One account may be owned by an international student, the other one owned by a person who is under heavy debts or it may be a joint account between two business partners. So, instead of creating fixtures inside test methods all over the code, we can set up fixtures using a factory class named Object Mother which is responsible to give birth to different test fixtures. We can have different object mothers based on our needs, such as EmployeMother, ClientMother, AccountMother, etc.
As always nontechnical consequences of technical approaches are mind-blowing. So, you may ask what can Object Mother bring to our workflow? As a matter of fact, using object mother pattern can help us to become more productive in writing and reading tests and even understanding the domain. From a non-technical perspective, using fixed test fixtures gradually leads to the notion of personas. By assigning a good descriptive name to test fixtures, we’ll end up with entities with unique identity and characteristics for each. As a result, each test fixture resembles a unique scenario in reality which team members can make a connection with. So, tests become more readable once we all know all about test personas. Even, the team can have constructive discussions about what’s happening inside tests and business scenarios using personas as an example.
Like design patterns, naming test fixtures through personas can help us to shape a better understanding of the system and to be able to discuss complex situations with others in less pain.
