TDD Best Practices: Don’t Mock Others
One design principle which is particularly relevant to the practice of using test doubles (mocks, stubs, spies, fakes, etc.) is the Dependency Inversion Principle. Stated simply, the Dependency Inversion Principle pertains to the decoupling of the stuff you most care about (like your domain layer) from the stuff you care least about (like your low-level infrastructure code and third-party libraries). In relation to the practice of using test doubles, it’s the coupling to other people’s stuff that is most relevant.
When you provide test doubles for types you don’t own, this indicates that you have a Dependency Inversion Principle violation. If you’re injecting concrete, abstract, or interface types for a third party dependency then that means your stuff can’t be used without their stuff. While not ideal, you might ignore this design principle due your personal stance of: “They’ll pry framework XYZ from my cold, dead hands”. Fair enough. Nevertheless, there are a few other reasons why you might want to consider following this principle.
When using test doubles for third-party libraries, you make assumptions about how the third-party library works. Perhaps the library you’re using has no bugs and you thoroughly understand all of its behavior, so you know that the behavior you are substituting behaves exactly like the real library will work in production once you put it all together. If you aren’t confident of this, however, you might find that your design doesn’t interact with the third-party component in quite the way you imagined. Let’s say it does though. What about the next version? When we design our systems using test doubles for third-party libraries, we create a false sense of security around the soundness and stability of our design.
Another problem caused by the use of test doubles for third-party components is the limitation it places on emergent design. Coupling to third-party components will often lead to making design concessions to accommodate your dependencies rather than allowing your own design to emerge through the feedback received through the TDD process.
If we shouldn’t couple our design to third-party libraries (thereby removing the need to supply test doubles for third-party libraries), how then do we make use of such libraries and ensure our code works correctly? The answer is to write component/unit level tests which guide the design of your own code which expresses its needs through its own interfaces (whose behavior is defined through the use of test doubles), and write integration tests which validate the behavior of adaptors which implement your design’s interfaces using the desired third-party components.
For example, you might design your components to rely upon an ILogger, IMapper, or IBus using test doubles to define their expected behavior and have integration tests which validate the implementation of these interfaces with log4net, AutoMapper, or ØMQ respectively.
In summary, don’t use test doubles for types you don’t own. Instead, let any dependencies you take on be an outgrowth of your emergent design and provide integration tests to validate the expected behavior of any third-party libraries you use to facilitate the required behavior.