Skip to content
Latchkey

What Is a Test Double?

A test double is any object that substitutes for a real dependency during testing so the code under test can be exercised in isolation. The family includes stubs (return canned values), mocks (assert on interactions), fakes (lightweight working implementations), and spies (record calls). They remove slow, nondeterministic, or unavailable dependencies from a test.

Why it matters

Real dependencies, databases, payment gateways, third-party APIs, make tests slow, flaky, or impossible to run in CI. Test doubles replace them with controllable substitutes so unit tests stay fast and deterministic. The trade-off is that over-mocking can make tests pass while the real integration is broken.

Related concepts

  • Includes stubs, mocks, fakes, and spies
  • Isolates the code under test from dependencies
  • Over-mocking can hide real integration failures

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →