LESSON 03 / 4·2 min readFoundation

Mock vs stub vs spy vs fixture?

01 Understand02 Recall03 Prepare

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.

PAUSE & RECALL

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.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Prefer checking the resulting behavior; asserting every internal call makes harmless refactoring break tests.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Is 100% coverage enough?

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.