Repaso
EspaƱol
TestingCoreTST-27

In testing, what is a mock?

Options

  1. A failing test that is written before the code
  2. A fake object that stands in for a real dependency (Correct option)
  3. A copy of the production environment for testing
  4. A very slow test that runs once in a while

Answer

A fake object that replaces a real dependency (like an API client) and records how it was used

Why

Real dependencies can be slow, cost money, or be unpredictable (sending an email, charging a card, calling an API).

A mock stands in for them during a test: you decide what it returns, and afterwards you can check how your code called it, e.g. 'was send_email called once with this address?'.

In Python: unittest.mock.Mock and patch.

Remember

Mock = stand-in that you control and inspect.

Key terms

Practice this question in Repaso