C# Interview Question #182
What is the difference between a mock, stub, fake, and spy?
Testing, Security, APIs & Reliability Senior Advanced
Quick Interview Answer
These are forms of test doubles used to replace real dependencies during testing.
Detailed Explanation
These are forms of test doubles used to replace real dependencies during testing.
A stub returns predefined data needed by a test. A mock is commonly configured with expectations and can verify that specific interactions occurred. A fake provides a working but simplified implementation, such as an in-memory repository. A spy records calls so tests can inspect how a dependency was used.
Terminology varies between testing communities and libraries. The important interview point is to choose the simplest test double that supports the behavior being tested and avoid over-coupling tests to implementation details.