The Wire

Fakes Over Mocks

A fake implements the same contract as the real thing, which makes tests easier to read.

A hand-written fake is often more useful than a generated mock because it gives the test a real, if simplified, implementation to talk to. The fake can store values in memory, return predictable errors, and make assertions about state instead of about call sequences. Tests then describe outcomes rather than choreography.

Fakes also age better. When the contract changes, the fake fails to compile and the test author has to think about the new behavior. That friction is a feature. It keeps the test suite aligned with the production code instead of letting stale expectations accumulate in mock configurations.

Arrived from