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 | 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.
2000s — Early testing frameworks blur "mock" and "fake"
2007 — Meszaros formalises Fake as one of five doubles
2007 — Fowler's Mocks Aren't Stubs cements the taxonomy
2010s — H2, SQLite in-memory, WireMock popularise fakes at scale
2019 — MSW (Mock Service Worker) brings fakes to the browser
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
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
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
MSW (2024). Mock Service Worker. https://mswjs.io/docs/
Etymology
Origin: inherited
fake(verb): from 19th-century underworld slang, "to rob"; adopted for "to counterfeit, to produce a sham"
Semantic network
● broader ● narrower ● related
Used in the garden
- Programmatic Advertising and AdTech: The Complete Guide (5×)
- How to Validate a SaaS Idea Without Writing Code (4×)
- I Made a Viral Video and Almost Destroyed My Brand (And How I Fixed It) (2×)
- 10 Forbidden Websites: The Web's Strangest Experiments (2026) (1×)
- Beneath the Surface: The 10 Creepiest Digital Artifacts of the Internet's Shadow World (1×)
- Automate to Liberation: The 5‑Tool Stack That Runs Your Business (1×)
- Consumer Psychology and Neuromarketing: The Complete Guide (1×)
- Influencer Marketing: How to Choose and Work with Creators (1×)
- 10 Forbidden Websites That Break Reality (2026 Edition) (1×)
- Your Brand Is Not a Logo: The Hidden Architecture of Trust (1×)
- Library Genesis: The Grandfather of Shadow Libraries (1×)
- A Reputation Crisis Almost Broke Me (And What I Learned) (1×)
- Digital Trust: How Branding and Marketing Build the Currency of the Internet (1×)
- Uncover Hidden Gems: Real Money Opportunities in Investor Hubs (1×)
- 10 Illegal Websites That Are Still Online (And What They Teach About Business) (1×)
- Z-Library Is Still Alive: The 2025 Access Guide (1×)
- 10 Forbidden & Weird Websites You Can Still Visit (2026 Guide) (1×)
- The Zoom Call That Was Actually a Wedding (1×)
- The Science of Pricing for Digital Products (1×)
- The Rapid Secret to Founding Profitable Businesses NOW! (1×)
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
MSW (2024). Mock Service Worker. https://mswjs.io/docs/