All courses

Test doubles: stubs, fakes, mocks and spies

Distinguish the four kinds of test double: stubs return canned answers, fakes are working lightweight implementations, mocks verify interactions and spies record calls. Say what each kind lets a test verify, and what it lets a test silently skip.

Read it card by card Start the whole course

Green tests around a payment call

You asked the agent to add tests for `checkout()`, which charges a card through a payment gateway. A minute later it reports a green suite and says checkout is "fully tested". Every test replaces the gateway with a stand-in. Before you trust that green, ask two things. What kind of stand-in is it? What could go wrong in `checkout()` that this test would never notice?

The double decides what the test can see

A **test double** replaces a real dependency (network, database, clock) so a test runs fast and on its own. Its kind decides what the test can observe. The agent keeps working until the tests are green. That is a proxy signal: a visible stand-in for the real goal, which is correct code. The agent only ever sees what the double returns, never the real gateway. So a double that always answers "success" lets code that charges the wrong amount, or ignores a failed charge, pass. The later cost: a wrong charge or an ignored failure ships behind a green suite.

Four kinds: what each checks and skips

- **Stub**: returns canned answers. Checks your code's output given that answer. Skips how your code called the dependency, and how the real one would answer. - **Fake**: a working lightweight version, such as an in-memory store. Checks behaviour across many calls, with real state. Skips wherever it differs from the real thing. - **Mock**: set up with expected calls, and fails if they don't happen. Checks interactions. Skips what your code does with the answer, unless the test also asserts on output. - **Spy**: records calls so you can assert on them afterwards. Checks only the calls you assert on. Skips every call and argument you don't.

Judge by use, not by library name

Library names don't follow the four kinds, so judge a double by how the test uses it. In one agent-written Java test, `when(repo.find(7)).thenReturn(order)` uses a Mockito `mock` as a stub. In the same test, `verify(mailer).send(any())` uses one as a mock. It checks that a send happened, but never what was sent. Mockito's `spy()` and Jest's `jest.spyOn` call the real method by default. So a "spy" on a real gateway can still send real charges. A fake is only as faithful as its author: an in-memory map has no unique constraints.

Stub only vs stub plus a call check

Python's `Mock` becomes whatever kind of double the test uses it as. Here `cart_of(4999)` builds a 4999-cent cart in EUR. In BAD, `gw` is a pure stub. The test proves `checkout` marks the order paid when the gateway says ok. Nothing checks what was charged, so charging 0, or 49.99 instead of 4999 cents, stays green. Later cost: real customers are charged the wrong amount. BETTER adds a spy-style assert after the call that pins amount and currency. A separate test should stub a failed charge.
// Python
# BAD
gw = Mock()
gw.charge.return_value = {"ok": True}
order = checkout(cart_of(4999), gw)
assert order.status == "paid"

# BETTER
gw = Mock()
gw.charge.return_value = {"ok": True}
order = checkout(cart_of(4999), gw)
assert order.status == "paid"
gw.charge.assert_called_once_with(
    4999, "EUR")

Ask what the double lets the test see

Stubs check your output for a scripted answer. Fakes check behaviour against a simplified stand-in. Mocks and spies check calls. Two things are never checked. Every answer the double scripts in place of the real dependency. Every call nobody asserts on. That gap is where a green suite hides wrong code.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.