I started working on a new project some time ago, and decided to work in a TDD style as much as I could. The idea was that there were very vague specifications for the project, resulting in a very “agile” project. So what methodology other than XP Programming and TDD should I adopt, I thought.(If you don't know about TDD (Test Driven Development) read about it on wiki.)
I have had some experience with TDD before, but I usually stopped writing tests when I started to develop the user interface. Although I used the MVP design pattern, so I should have been able to test the presenter using a mock as a view, I think this is a very time consuming process, and sometimes maybe not very useful, especially when the presenter logic is not very complex – it takes data from the object and passes it to the view.
The project I was talking about is a monitoring engine. Used to monitor computers, software applications and other stuff… The business was pretty complex, and there was no user interface, so the TDD was almost a must. I started to work in almost in a perfect TDD style, but after some time I found it very difficult to maintain the unit tests.
The problem was that a small change in the business code determined a lot of changes in the unit tests. It was pretty upsetting when seeing 100 compile errors when changing just the number of parameters of a constructor. And very time consuming. At some point, 50% of my work consisted in maintaining the unit tests. Most of my tests were constructing business objects in order to test them. A small change in the constructor or in the manner the business object was constructed determined as I said a lot of errors in the tests. So my idea was to separate the object construction from the unit tests, and make some kind of object factory, but a smart factory, which could create very complex objects with just a function call, or let me customize the object creation.
I found that this method is in fact a pattern, called “Object Mother”:
“Through a handful of simple method calls, this utility provided a complete, valid, and customizable structure of business objects (think of an invoice: its lines, all related charges, remit to, bill to).
Object Mother starts with the factory pattern, by delivering prefabricated test-ready objects via a simple method call. It moves beyond the realm of the factory by
- Facilitating the customization of created objects,
- Providing methods to update the objects during the tests, and
- If necessary, deleting the object from the database at the completion of the test.
Now, when I change the number of parameters in a constructor, I have to change only the object mother not all my 1000 test units, so I consider this method very helpful.