Software engineering / Mocks, stubs and fakes

Mocks, stubs and fakes

One checkout function with three dependencies, each replaced in turn by a different kind of stand-in, to show what a stub, a mock and a fake are for and what goes wrong when everything is mocked.

Intermediate7 steps~7 min
Code under test — the one piece the test exercises
Stub — returns canned answers
Mock — records calls for the test to verify
Fake — a simple implementation that really works
// THE SHORT VERSION

In 30 seconds

To test one piece of code alone, you replace the things it depends on with stand-ins called test doubles.

A stub returns prepared answers, so the test can check the result. A mock records how it was called, so the test can verify the calls. A fake is a simple working version, such as a database kept in memory.

Mocks are easy to overuse. A test built only from mocks checks which calls the code makes, not whether it works, and breaks whenever the code is reorganised.

Mocks, stubs and fakes, step by step

STEPS01 / 07

Names vary: many libraries call every double a mock, and strictly a double that only records calls for later checking is a spy. Code is JavaScript with Jest-style assertions; setup is omitted.

STEP 01 / 07 Code with real helpers
THE MATH
cart.sum = 30, tax = 3 total = 30 + 3 = 33 then save it, then email it
  1. Step 1: Code with real helpers. checkout() does little on its own: it asks a tax service for the tax, saves the order in a database and emails a receipt. A test that used the real three would need a network and a database server, would be slow, could fail for reasons unrelated to checkout, and would send a real email on every run.
  2. Step 2: Swap in a test double. A test double is any object that takes the place of a real dependency during a test, as a stunt double stands in for an actor. It offers the same methods, so checkout() calls it exactly as before and cannot tell the difference. Stub, mock and fake are three kinds of double, and they differ in what happens inside.
  3. Step 3: Stub: canned answers. A stub has no logic. It returns a fixed, prepared answer whatever it is asked: here the tax is always 3. That gives the test a known input, so it can check the result checkout() produces: 30 + 3 = 33. The test never asks the stub anything afterwards. It only looks at what came out.
  4. Step 4: Mock: verifies the calls. Sending an email returns nothing useful to check. What matters is that it was sent, once, to the right person. A mock records every call made to it, and the test then verifies those calls: send was called with Ana’s address and the total 33. The assertion is about the conversation, not about a result.
  5. Step 5: Fake: a small working version. A fake really does the job, in a simplified way. This fake database keeps orders in an in-memory map instead of on a server: save stores, find retrieves, exactly as the real one would. It takes a few lines, runs instantly and forgets everything when the test ends, which is also why it could never be used in production.
  6. Step 6: Three doubles, side by side. The three answer different needs. Use a stub when the code needs an input from a dependency. Use a mock when the outcome is an action on the outside world that leaves nothing else to inspect. Use a fake when the code leans on real behaviour, such as storing and finding, and a simple version is cheap to write.
  7. Step 7: The danger of over-mocking. Replace everything with mocks and the test stops checking behaviour: it only checks that the code makes the calls you scripted. Rename save and the test breaks although nothing is wrong. Let the real tax service change its answer and the test stays green while checkout is wrong. Mock only what is slow or outside your control.
VALUES SO FAR
cart.sum30input
double····test
stub····canned
total····result
mock····verified
fake····works
risk····brittle
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

// AFTER

Learn next

Nothing builds on this one yet. See every software engineering concept.

Spotted a mistake on this page?