Fakes, Stubs and Mocks
A test double stands in for a real dependency. The names get used loosely; the distinctions that matter in practice are these.
A stub returns a canned answer. A fake is a working but simplified implementation, such as an in-memory store. A mock additionally records how it was called, so the test can assert on the interaction.
Stubs and fakes are usually the right choice. Mocks assert on how something was done rather than what happened, which is testing implementation, and that is the brittleness people blame on mocking in general.
What genuinely deserves a double. Anything slow, anything you do not control, anything with a side effect you do not want, and anything that is hard to put in a specific state.
Third-party APIs. Sending email or charging a card. The clock, when behavior depends on time. Randomness. A rate-limited service.
What does not. Your own database, in a project this size: the real test database is fast, and using it catches the wrong query, which a fake never will.
Mark this lesson complete
Signed in, your progress follows you to every device.