Mock vs stub vs spy vs fixture?
Understand the concept
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.
Important ideas
-
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.
Can you explain it without the notes?
Mock vs stub vs spy vs fixture?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
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.
Turn this concept into a clear answer.
- 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?”. Examples are study fragments; framework behavior can depend on your version.