Dummies for parameters you must pass, stubs for canned input, spies for post-hoc verification, mocks for contracts, fakes for slow dependencies.
Picking the right double
Dummy: when you must pass something
A logger parameter that the test never triggers. Pass an empty object; never call it.
Stub: when you need a specific return
A clock that returns
2024-01-01T00:00:00Zdeterministically. Canned, no logic.Spy: when you need to assert afterwards
A mailer that logs what it sent; the test asserts on the log.
Mock: when the contract matters
A payment gateway that must be called exactly once with amount X. The mock fails the test if not.
Fake: when the dependency is heavy
An in-memory database that behaves like Postgres for the queries the test runs.