Skip to content

How to use one without regretting it

Mock collaborations, not data; keep expectations minimal; never mock what you do not own.

A mock that survives refactoring

  1. Mock the boundary

    Mock the external collaborator (the payment gateway), not the internal helper.

  2. Expect the minimum

    One call with one argument is a contract. Twelve calls with ordered arguments is a straitjacket.

  3. Prefer state when possible

    If the final state is enough, use a stub and assert the state.

  4. Never mock third-party SDKs directly

    Wrap them in an adapter and mock the adapter. The SDK will change; your test should not.