Extracting a factory method for creating data objects
It’s time to simplify the Arrange phase of the tests using a factory method named createBirthday
.
The last section mentioned how each of the Arrange-Act-Assert phases needs a different treatment for simplification. A key method for the Arrange phase is the use of factories. You already created one of those in Chapter 4, Saving Form Data. That was the createFormDataRequest
method that you used in the preceding section.
Using test factories to hide irrelevant data
Factory methods help you generate supporting objects in the shortest amount of code possible. One way they do that is by setting default values for object properties so that you don’t need to specify them. You’re then free to override those defaults in each individual test.
Hiding necessary but irrelevant data is a key method for keeping unit tests succinct and clear.
Our birthday objects have a very simple structure, with just three fields...