What Is a Test Double?
A test double is any object that stands in for a real dependency in a test, including mocks, stubs, spies, fakes, and dummies.
Test double is the umbrella term, borrowed from the idea of a stunt double, for anything that replaces a real collaborator during testing. Mocks, stubs, spies, fakes, and dummies are all test doubles. Knowing the family helps you pick the lightest tool that does the job.
Why use a double at all
Real dependencies can be slow, nondeterministic, expensive, or simply unavailable in a test environment. A double replaces them with something fast and controllable, so the test can focus on the code under test rather than the behavior of the outside world.
The five kinds
- Dummy: passed around but never actually used.
- Stub: returns canned answers.
- Spy: records how it was called, often wrapping real behavior.
- Mock: pre-programmed with expectations it verifies.
- Fake: a real but simplified implementation, like an in-memory store.
A quick example
An in-memory repository is a fake: a working implementation simple enough to run in a test without a real database.
class FakeUserRepo {
constructor() { this.users = new Map(); }
save(u) { this.users.set(u.id, u); }
find(id) { return this.users.get(id); }
}Choosing the right double
Use the simplest double that works. A dummy or stub is enough when you only need a value; a spy or mock when you must verify an interaction; a fake when you need realistic behavior without the real dependency. Reaching for a heavyweight mock when a stub would do makes tests brittle.
Test doubles and CI speed
Doubles strip out I/O and external services, which is why unit tests using them run fast and rarely flake in CI. The remaining flakiness sits in tests that use real dependencies, where Latchkey auto-retries genuinely transient failures so a flaky external call does not red-flag a good build.
Key takeaways
- Test double is the umbrella term for mocks, stubs, spies, fakes, dummies.
- Each kind trades realism for control differently.
- Pick the lightest double that verifies the behavior you care about.