Testing Patterns & TDD Vocabulary
5 exercises — mock/stub/spy/fake/dummy test doubles, TDD red-green-refactor discipline, property-based testing, diagnosing flaky tests, and consumer-driven contract testing with Pact.
0 / 5 completed
Testing vocabulary quick reference
- Dummy = unused placeholder · Stub = returns canned data · Mock = verifies interactions · Spy = real + records calls · Fake = working simplified implementation
- TDD: Red (failing test first) → Green (minimum code to pass) → Refactor (improve, stay green)
- Property-based testing = define invariants, framework generates 100s of random inputs; tools: Hypothesis, fast-check
- Flaky test = non-deterministic; causes: time dependency, shared state, real network, test ordering, async race conditions
- Contract testing / Pact: consumer writes the contract · provider verifies it · no joint deployment needed
1 / 5
A test review says: "This is a mock, not a stub. And we shouldn't be using a fake here." What are the five test double types and when do you use each?
Test doubles vocabulary (Gerard Meszaros's taxonomy):
Dummy
Passed as an argument but never actually used in the test. Just fills a required parameter.
Example:
Stub
Returns hard-coded or pre-configured answers. Does NOT verify how it was called.
Example:
Use when: you want to control indirect inputs (return values, exceptions thrown).
Mock
Pre-programmed with expectations. Verifies that the expected calls were made with the expected arguments.
Example:
Use when: the test is verifying that the system under test called a collaborator correctly.
Failure mode: test fails at mock.verify() if expectations were not met.
Spy
Like a mock but set up after the call. Records what happened without pre-setting expectations.
Example:
Use when: you want to observe calls to a real object without completely replacing it.
Fake
A working, simplified implementation of a real dependency.
Examples: in-memory SQLite database (instead of PostgreSQL), in-memory message broker, test SMTP server.
Use when: integration-level tests need a fast real-like implementation.
Rule of thumb:
• "Don't mock what you don't own" — only mock collaborators you control
• Over-mocking leads to tests that pass but don't test real behavior
• Prefer stubs/fakes for infrastructure; prefer mocks for verifying key interactions
Vocabulary:
• test double — umbrella term for any stand-in for a production object
• system under test (SUT) — the class or function being tested
• collaborator — objects or services the SUT interacts with
• indirect input — data supplied to the SUT through a collaborator
• indirect output — side effects or calls the SUT makes to a collaborator
Dummy
Passed as an argument but never actually used in the test. Just fills a required parameter.
Example:
new UserService(logger = NullLogger()) — the test doesn't care about logging.Stub
Returns hard-coded or pre-configured answers. Does NOT verify how it was called.
Example:
userRepo.findById.returns(testUser) — control input to the unit under test.Use when: you want to control indirect inputs (return values, exceptions thrown).
Mock
Pre-programmed with expectations. Verifies that the expected calls were made with the expected arguments.
Example:
emailService.expects("sendWelcomeEmail").with(user).once()Use when: the test is verifying that the system under test called a collaborator correctly.
Failure mode: test fails at mock.verify() if expectations were not met.
Spy
Like a mock but set up after the call. Records what happened without pre-setting expectations.
Example:
const spy = jest.spyOn(emailService, "send"); // assert afterUse when: you want to observe calls to a real object without completely replacing it.
Fake
A working, simplified implementation of a real dependency.
Examples: in-memory SQLite database (instead of PostgreSQL), in-memory message broker, test SMTP server.
Use when: integration-level tests need a fast real-like implementation.
Rule of thumb:
• "Don't mock what you don't own" — only mock collaborators you control
• Over-mocking leads to tests that pass but don't test real behavior
• Prefer stubs/fakes for infrastructure; prefer mocks for verifying key interactions
Vocabulary:
• test double — umbrella term for any stand-in for a production object
• system under test (SUT) — the class or function being tested
• collaborator — objects or services the SUT interacts with
• indirect input — data supplied to the SUT through a collaborator
• indirect output — side effects or calls the SUT makes to a collaborator