Test Doubles Guidelines
Only Use Test Doubles for Classes That You Own
Using Test Doubles is a technique for software design. That's why it is important to only mock/stub types that belong to the team's code base. The key concept here is that if the Test Doubles are coupled with an external interface, they cannot change the design to respond to requirements that arise from the process.
The same is valid for fixed types, such as those defined by the runtime or external libraries. So, how do we test the interaction with an external library? The trick here is to write a thin wrapper to implement the abstractions for the underlying infrastructure. Those wrappers are the components belonging to the team's code base that:
- Are generated from a behavior-first position (public interface definition first)
- Can be substituted with Test Doubles
We have found this to be great insight for understanding the technique. It restores the pre-eminence of design in...