Overview
Test doubles replace collaborators during a test. Common forms include stubs that provide canned responses, fakes with lightweight working behavior, spies that record interactions and mocks that verify expected interactions.
Choose by purpose
Use a stub when the return value is what matters. Use a fake when a small in-memory implementation is clearer. Use a mock when a specific collaboration is part of the contract, such as ensuring a payment request is not sent after validation fails.
Avoid over-mocking
Mocking every method call couples tests to implementation details. A harmless refactor can then break tests even when externally visible behavior remains correct.
Keep doubles realistic
A fake repository should respect the important semantics of the real repository, such as uniqueness and lookup behavior. It is not a substitute for integration tests against the actual database.
Review each expectation
Every interaction expectation should explain a meaningful requirement. If removing it would not weaken the test, it may be unnecessary.