Test doubles: dummy, stub, spy, mock, fake — what's the difference?
Essence
A test double is any object that stands in for a real dependency in a test (Gerard Meszaros's term). The five kinds differ in what they do with calls:
- Dummy: a placeholder to fill a parameter; never actually used (
UIView()instead of a real container). - Stub: returns predefined answers; controls the inputs for the code under test.
- Spy: records how it was called (arguments, counts); the test does the checking.
- Mock: knows in advance which calls to expect and verifies them itself; the failure comes from the mock.
- Fake: a working simplified implementation: an in-memory database instead of SQLite, a dictionary instead of
UserDefaults.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep