Mock vs stub vs spy vs fixture?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Mock vs stub vs spy vs fixture?
Definition:
Test doubles replace real collaborators so a test can control conditions or observe interactions. A stub supplies a response, a spy records calls, and a mock often combines replacement with expected interactions; terminology varies by library. Fixtures supply stable input data so cases are repeatable.
Key points:
-
Stub: controlled behavior; spy: records calls; mock: replaces a dependency and may verify interactions.
-
Fixture: repeatable test data.
-
These terms overlap between tools; test observable behavior instead of every internal call.
-
Reset changed mocks between tests and avoid mocking the exact logic the test is meant to verify.
Interview point:
- Prefer checking the resulting behavior; asserting every internal call makes harmless refactoring break tests.
Adapted from 09_TESTING_GIT_DOCKER_CLOUD.md · “Mock vs stub vs spy vs fixture?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.