Frequent questions: when to use a fake, how it differs from a stub, and how to keep it in sync.
- When should I use a fake?
When the collaborator is a well-defined interface with stable behaviour and the test needs to exercise a workflow, not a single branch. In-memory repositories, fake HTTP servers, frozen clocks.
- Fake vs stub?
A stub returns a canned answer; a fake works. The stub checks a branch ("what if find returns X?"); the fake checks a workflow ("what if the repository works?").
- How do I keep a fake in sync with the real one?
Two mechanisms: (1) implement the interface with a type check that fails at compile/test time when the interface changes, (2) run the fake against the same acceptance suite as the real one periodically. A fake that diverges silently is worse than no fake at all.
- Can a fake be used in production?
No. By definition, the fake trades completeness for simplicity. It leaks into production, it hides bugs, and it is not what the interface promises. Its place is the test suite.