Skip to content

Fake

A fake is a test double with a working but simplified implementation of a collaborator: an in-memory database instead of the real one, a stub clock instead of the system clock.

Definition

What it is

The canonical example is an in-memory repository that replaces a real SQL database. The fake implements the same interface — save, find, delete — but stores records in a dictionary. The test runs the same code path as production; the database is replaced by a Map.

class InMemoryUserRepository implements UserRepository {
    private array $users = [];
    public function save(User $u): void { $this->users[$u->id] = $u; }
    public function find(string $id): ?User { return $this->users[$id] ?? null; }
}

The distinction from the other doubles is implementation, not behaviour. A stub returns a canned answer; a mock asserts an expectation; a spy records calls; a fake works, just less completely than the real thing.

Why it matters

A test with a stub checks "what happens when find returns X". A test with a fake checks "what happens when the repository works". That difference is significant: the first tests a branch, the second tests a workflow. Martin Fowler argues that fakes are usually the best choice for state verification (checking results), while spies/mocks are best for interaction verification (checking calls) [1].

The cost is maintenance. A fake is production code that happens to live in tests: it must stay in sync with the real interface, it must implement the same contract, and it must not be used in production where its shortcuts become bugs. Martin Fowler's Mocks Aren't Stubs (2007) and the broader "classicist vs mockist" debate in the TDD community settled on a pragmatic line: fakes where behaviour is stable and complex, mocks where the collaboration matters more than the outcome.

How it works

graph LR
  P[Production code] --> I[Interface UserRepository]
  I -->|prod| R[Real SQL repository]
  I -->|test| F[In-memory fake]
  R --> DB[(PostgreSQL)]
  F --> M[Map in memory]
  style F fill:#E8F5E9
  style M fill:#E8F5E9

Three well-known fakes in the wild. H2 and SQLite in-memory replace a real database for integration-style tests. Fake HTTP servers (WireMock, MSW) implement the wire protocol of a partner service. Time fakes (Clock.fixed, freeze_time) replace the system clock so tests that depend on "now" become deterministic. Each one trades a slice of production realism for test speed, reliability and isolation.

Fake vs stub vs mock

Aspect

Stub

Spy

Mock

Fake

Behaviour

Canned answer

Delegates to real

Canned + asserts

Real, simplified

Records calls

No

Yes

Yes

Depends

Fails on its own

No

No

Yes

No

Production-realistic

Low

High

Low

Medium

Maintenance cost

Low

Medium

High

High

Common failures

Origin

Before the 2000s, the vocabulary was loose: developers said "mock" for everything, "stub" for anything that returned data, and "fake" for the production substitute in integration tests. Meszaros' book codified the five-double taxonomy (dummy, stub, spy, mock, fake), and Fowler's article popularised it in the agile/XP community. The fake got its own name because it was doing something no other double could: it replaced a collaborator with a functionally equivalent but simplified implementation.

  1. 2000s — Early testing frameworks blur "mock" and "fake"

  2. 2007 — Meszaros formalises Fake as one of five doubles

  3. 2007 — Fowler's Mocks Aren't Stubs cements the taxonomy

  4. 2010s — H2, SQLite in-memory, WireMock popularise fakes at scale

  5. 2019 — MSW (Mock Service Worker) brings fakes to the browser

  6. 2024 — "Testcontainers" offers real containers as an alternative to fakes

Fakes have working implementations, but usually take some shortcut which makes them not suitable for production.

— Martin Fowler, *Mocks Aren't Stubs*, 2007
2007 · Meszaros y Fowler formalizan el fake
5 · dobles de pruebadummy, stub, spy, mock, fake
1 · fake por test típico (in-memory DB, wire server, clock)
2-3× · mantenimiento más caro que un mock (fake = código productivo en tests)

Evidence

FAQ

When should I use a fake?

When the collaborator is a well-defined interface with stable behaviour and the test needs to exercise a workflow, not a single branch. In-memory repositories, fake HTTP servers, frozen clocks.

Fake vs stub?

A stub returns a canned answer; a fake works. The stub checks a branch ("what if find returns X?"); the fake checks a workflow ("what if the repository works?").

How do I keep a fake in sync with the real one?

Two mechanisms: (1) implement the interface with a type check that fails at compile/test time when the interface changes, (2) run the fake against the same acceptance suite as the real one periodically. A fake that diverges silently is worse than no fake at all.

Can a fake be used in production?

No. By definition, the fake trades completeness for simplicity. It leaks into production, it hides bugs, and it is not what the interface promises. Its place is the test suite.

Connections

Etymology

Origin: inherited

  • fake (verb): from 19th-century underworld slang, "to rob"; adopted for "to counterfeit, to produce a sham"

Semantic network

FakeTest doublsoftware-tStubSpyMockDummyTest doubl

● broader ● narrower ● related

Broader

Related

Used in the garden

Sources