All glossary terms
Verify

Test double

A test double is any object that stands in for a real dependency in a test — mock, stub, fake, spy, dummy. The 'double' umbrella term (Gerard Meszaros) avoids the precise-but-confusing distinctions between mock and stub; each variant has specific semantics about what it records and what it controls.

The five canonical doubles: dummy (passes through but isn't used), stub (returns canned responses, no assertions), fake (working implementation with shortcuts, e.g. in-memory database), spy (records calls for later assertion), and mock (pre-programmed with expectations, fails if not met). The choice matters: mocks couple tests to implementation; stubs and fakes couple tests to behaviour. Tests written with heavy mocking often break on refactors that don't change behaviour; tests written with stubs and fakes are more resilient. The pragmatic guidance: prefer the simplest double that meets the need (dummy < stub < fake < spy < mock).

Related terms