Repaso
EspaƱol
Design & PracticesIntermediateDP-17

What should a unit test usually mock?

Options

  1. The class under test, so that the test stays simple
  2. External boundaries: network, database, clock, random (Correct option)
  3. Pure helper functions that have no side effects
  4. Nothing at all, because mocks are bad practice

Answer

External boundaries: network, database, clock, random

Why

Mock things that are slow, unpredictable or outside your control: HTTP APIs, the database, the current time, random numbers.

Do NOT mock the code you are testing, or simple internal helpers: then the test only checks your mocks, and every refactor breaks it.

Remember

Mock the boundaries, not your own logic.

Key terms

Practice this question in Repaso