What should a unit test usually mock?
Options
- The class under test, so that the test stays simple
- External boundaries: network, database, clock, random (Correct option)
- Pure helper functions that have no side effects
- 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.