Am I the only one who does this?
I like to use Object Mothers in my test projects, especially when I have a large number of services or entities I need to feed into the things I am testing. For example, if I have a class I am testing that takes a constructor argument, I may set it up in my test like this:
Since I probably don’t want to pass in a real RecipeEntity I can obviously mock one. (Also, don’t freak over the fact that I am injecting a single entity into a controller. This isn’t the point of the post.)
And this is all old hat goodness. It turns out that I find I want to mock IRecipeEntity a lot. And because of that, I can put it up in my context class OR I can get the mock through an object mother.
This is especially useful when I need to build up a complex set of dependencies. So without further delay, here is a mother implementation that mocks up a complete entity.
And now my test reads like this:
Now, this is cool, but what if I want to make a bad entity? One that should make my test fail, for instance? Do I need another mother? Nope, I just use the one the mother created, an mess with it a little.
Now I can create all those boundary condition tests that SHOULD fail by starting with a good entity and then only changing the parts I want to cause to be invalid.