In testing, what is a mock?
Options
- A failing test that is written before the code
- A fake object that stands in for a real dependency (Correct option)
- A copy of the production environment for testing
- 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.