Kelly Mears

Test Double

A stand-in for a real dependency during a test — and the ways a stand-in can lie.

Testing & Verification2 min read207 words12 out · 14 in
also calledMockStubFake

A test double replaces a real dependency during a test. The taxonomy, from Gerard Meszaros, distinguishes stubs (return canned answers), mocks (assert on how they were called), fakes (working but simplified implementations), and spies (record calls for later inspection).

The unifying risk is that a double can be internally inconsistent in ways the real thing is not. The clearest example: a fake data store whose create operation returns a record that its find operation cannot see. Every individual response is plausible; together they describe an impossible world. Code under test then behaves reasonably and produces nonsense — an agent that verifies its own write, sees nothing, and writes again. The test recorded something, but not what it was measuring. See Evaluation Harness.

The rule that avoids most of this: make the double a small consistent model rather than a set of independent canned answers. A ten-line store where create, find, count, and update agree is usually less work than a pile of stubs, and it cannot lie about its own state.

The second risk is that a double drifts from the real interface, so the tests pass and production fails. Contract tests, shared type definitions, and preferring real implementations wherever they are cheap enough all reduce it.

See also11

Test Fixture

The prepared data or state a test runs against.

Testing & Verification16 connections

Record and Replay Testing

Capturing a real interaction once and replaying it deterministically in later test runs.

Agents & Language Models14 connections

Integration Test

A test exercising several components together, through real interfaces rather than doubles.

Testing & Verification17 connections

Fingerprint

A hash over everything that determined a result, used to detect when the result has gone stale.

Agents & Language Models12 connections

Unit Test

A fast, isolated test of one unit of behavior with its collaborators replaced.

Testing & Verification25 connections

Vacuous Truth

A statement that holds only because its subject set is empty — a green result that measured nothing.

Method26 connections

Nondeterminism

The property that identical inputs may produce different outputs, and what it costs to test around.

Agents & Language Models24 connections

Large Language Model

A neural network trained to predict text, used as a general-purpose instruction-following system.

Agents & Language Models38 connections

Service Virtualization

Replacing a real external service with a simulated one that speaks the same protocol over the network, so tests can run without the real dependency and can reproduce its failures on demand.

Testing & Verification15 connections

Fake Clock

Replacing the real clock in tests with one the test controls, so time-dependent code can be checked at midnight, across DST, or after an hour of retries without waiting.

Testing & Verification9 connections

Dependency Injection

Passing a component the things it depends on instead of letting it build them itself, which decouples code and makes it easy to substitute fakes in tests.

Testing & Verification6 connections

Linked from14