Definition
What it is
The term comes from the film industry: a "stunt double" replaces the actor for dangerous scenes. In software testing, a test double replaces a real dependency for cases where using the real one is impractical, slow, non-deterministic or impossible.
There are five flavors, each solving a different problem. This is the taxonomy formalized by Gerard Meszaros in xUnit Test Patterns (2007) and popularized by Martin Fowler's Mocks Aren't Stubs (2007).
The five doubles
Type | Returns | Records calls | Asserts expectations | Has logic |
|---|---|---|---|---|
Nothing | No | No | No | |
Canned answer | No | No | No | |
Canned answer | Yes | No (assertions do) | No | |
Canned answer | Yes | Yes | No | |
Computed answer | Sometimes | No | Yes |
graph TD TD[Test Double] --> D[Dummy] TD --> S[Stub] TD --> SP[Spy] TD --> M[Mock] TD --> F[Fake] D -.-> NoBehaviour[No logic, never used] S -.-> Canned[Canned answer] SP -.-> Records[Records, you assert] M -.-> Asserts[Asserts during test] F -.-> Logic[Working logic] style TD fill:#E8F5E9
Why it matters
A unit test is meant to test one thing: one function, one method, one unit. If that unit calls a database, a payment gateway or time.now(), testing it in isolation requires replacing those dependencies. Test doubles are the way. Without them, "unit test" would be a euphemism for "integration test that takes 50 ms and fails on Tuesdays".
When to use each
Picking the right double
Dummy: when you must pass something
A logger parameter that the test never triggers. Pass an empty object; never call it.
Stub: when you need a specific return
A clock that returns
2024-01-01T00:00:00Zdeterministically. Canned, no logic.Spy: when you need to assert afterwards
A mailer that logs what it sent; the test asserts on the log.
Mock: when the contract matters
A payment gateway that must be called exactly once with amount X. The mock fails the test if not.
Fake: when the dependency is heavy
An in-memory database that behaves like Postgres for the queries the test runs.
Common failures
Origin
The stunt double metaphor entered software through the Extreme Programming community in the early 2000s. Tim Mackinnon, Steve Freeman and Philip Craig described a "mock object" pattern at XP2000; Gerard Meszaros generalized it to "test double" in xUnit Test Patterns (2007). The metaphor is exact: a double performs the role of the original for the test, and returns to the wings when the scene is over.
Test Double is a generic term for any case where you replace a production object for testing purposes.
— Martin Fowler, *Mocks Aren't Stubs*, 2 January 2007
Evidence
FAQ
- How many test doubles?
Five flavors (dummy, stub, spy, mock, fake). Some frameworks blur the lines; the taxonomy is a mental model, not a class hierarchy.
- Do they replace integration tests?
No. Doubles prove the unit is correct in isolation; integration tests prove the system works when the real dependencies are in play.
- Are test doubles cheating?
Only if they lie. A well-designed double (a fake that behaves like the real thing, a stub that returns plausible input) makes tests more honest, not less.
- Should I always use the same framework?
The taxonomy is language-agnostic. Sinon, Jest, Mockito, PHPUnit, RSpec all implement the same five ideas under different names.
Connections
Meszaros, G. (2007). xUnit Test Patterns. https://martinfowler.com/books/meszaros.html
Fowler, M. (2007). Mocks Aren't Stubs. https://martinfowler.com/articles/mocksArentStubs.html
Etymology
Origin: compound
test(noun): from Latin testum, "earthen pot, vessel used in assaying"double(noun): from Latin duplus, "twofold"; film sense of "stunt double" from the 1930s
Semantic network
● broader ● narrower ● related
Sources
Meszaros, G. (2007). xUnit Test Patterns. https://martinfowler.com/books/meszaros.html
Fowler, M. (2007). Mocks Aren't Stubs. https://martinfowler.com/articles/mocksArentStubs.html