Be wary of container calls in tests
This one cost me a couple of hours today. To be clear:
No calls to the IoC container in a unit or integration test!
I want my tests to fail because of an assertion failure, not because of a setup failure. Containers aren’t brittle, but they can be in a test, and an incorrectly configured container can cause an explosion of failures in tests. That makes it very difficult to pinpoint a true failure. There are plenty of patterns to set up fixtures and contexts – from the Builder pattern to the Object Mother pattern to a common Setup method. All of these can set up the context of a test without using an IoC container.
I have tests for my container, but I have to be very, very wary of a container call in a test. And no, I don’t use an auto-mocking container as well, I have yet to be convinced it’s better than Builder or Object Mother. With those patterns, I can follow the yellow-brick road to exactly what’s being constructed. Not so in the event of a container failure in a test.
If you’ve had luck with containers in a test, I’d love to hear about it.