Skip to content

When to use each

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

  1. Dummy: when you must pass something

    A logger parameter that the test never triggers. Pass an empty object; never call it.

  2. Stub: when you need a specific return

    A clock that returns 2024-01-01T00:00:00Z deterministically. Canned, no logic.

  3. Spy: when you need to assert afterwards

    A mailer that logs what it sent; the test asserts on the log.

  4. Mock: when the contract matters

    A payment gateway that must be called exactly once with amount X. The mock fails the test if not.

  5. Fake: when the dependency is heavy

    An in-memory database that behaves like Postgres for the queries the test runs.