Blog

Mock vs Stub vs Spy vs Fake: The Test Double Vocabulary Nobody Agrees On

September 17, 2026Xerat024 min read
Share
Mock vs Stub vs Spy vs Fake: The Test Double Vocabulary Nobody Agrees On

"I mocked out the database" almost never means someone used an actual Mock object. It usually means they used a stub, or a fake, or occasionally a spy — and the fact that "mock" has become the generic word for "fake thing standing in for a real thing" is exactly the confusion this post is trying to undo, because the specific terms encode a real, useful distinction about what a passing test actually proves.

The vocabulary below comes from Gerard Meszaros's xUnit Test Patterns, popularized by Martin Fowler's essay "Mocks Aren't Stubs" — it's not used with perfect consistency across every codebase, but it's the closest thing the industry has to a shared standard, and the underlying distinctions hold up regardless of which exact word a given team reaches for.

The five terms

Dummy — an object passed around to satisfy a parameter list, never actually used for anything. A null, or a placeholder object, handed to a constructor because the signature requires something there, even though the test doesn't care what.

Fake — a working implementation that takes a shortcut unsuitable for production. An in-memory list standing in for a database, a hand-rolled HTTP server standing in for a real API. It genuinely does the thing — reads and writes actually work — just not in a way you'd ship.

Stub — canned answers to specific calls, nothing more. Call getUser(42) on a stub and it returns the fixed object you told it to; call anything you didn't program it for and it either does nothing or breaks. A stub doesn't pretend to have real behavior — it just returns whatever the test needs to reach the code path being tested.

Spy — a stub with a memory. Same canned-answer behavior, plus it records what was called and with what arguments, so the test can inspect afterward: "was sendEmail called exactly once, with this address?" A spy lets the test verify something happened, after the fact.

Mock — pre-programmed with expectations, not just answers. A mock is told in advance what calls it should receive, and the test itself fails if those expectations aren't met — the verification is built into the double, not bolted on afterward by the test inspecting a spy's recorded history.

The one distinction that actually matters day to day

Fowler's real point, the one worth remembering even if the five-way taxonomy doesn't stick: everything except a true Mock uses state verification — the test runs, then separately checks that the resulting state (a return value, a database row, a spy's recorded calls) looks right. A Mock uses behavior verification — the double itself asserts that it was called correctly, as part of running.

That distinction has a real consequence: behavior-verifying tests (real Mocks) couple the test to how the code achieves its result, not just what result it achieves. Refactor the implementation to call a dependency in a different order, or not call it at all because you found a better approach, and a Mock-based test can fail even though the outward behavior is completely unchanged — which is the classic complaint about "mocking everything": the tests get brittle, not because mocking is bad, but because behavior verification was used somewhere a state-verifying stub or fake would have tested the same thing without caring about implementation details.

Where this connects to testing an actual API dependency

Most of the terms above describe doubles for objects inside your own process — a repository interface, a service class. A hosted mock API is closer to a Fake in this taxonomy once it has stateful routes turned on (a real, working implementation, just not the production one) and closer to a Stub when it's just returning fixed responses per route — the object-level vocabulary above still applies at the network boundary, just with an HTTP request standing in for a method call. Knowing which one you're actually building helps decide what the test is allowed to assume: a stubbed response tells you your code handles that exact shape correctly; it tells you nothing about whether the real API would ever actually produce it, which is a different, and equally real, kind of confidence than the terms above are meant to distinguish.

X

Xerat02

Building MockBase.