Testing · English usage comparison
Mock vs Stub: English Usage Guide for IT Professionals
Both are test doubles that replace real dependencies, but they differ in what they verify. A stub provides pre-programmed responses; you don't assert on it. A mock also records interactions and allows assertions on how it was called. The distinction matters in conversations about test design.
Side-by-side comparison
| Aspect | Mock | Stub |
|---|---|---|
| Purpose | Verify behaviour — assert it was called correctly | Provide canned responses — no interaction assertions |
| Assertions on it? | Yes — you check calls, arguments, count | No — it just returns values |
| Fails the test if? | Expected calls don't happen | Never directly (it just returns data) |
| Common usage | "Mock the HTTP client and verify the header was sent." | "Stub the DB to return a fixed user object." |
Example sentences
Mock
- "We mock the email service to verify it was called exactly once with the correct recipient address."
- "The mock records all calls — the test fails if sendEmail() was never invoked."
Stub
- "We stub the database to return a fixed user object so the test doesn't need a real DB."
- "The stub always returns { id: 1, name: 'Alice' } regardless of input."
Exercises: choose the correct English usage
Select the best answer for each question, then check your reasoning.
1. You replace the database with an object that returns fixed data. You don't check how it was called. This is a ___.
Explanation: Providing canned responses without interaction assertions = stub.
2. You replace the email service with an object and assert it was called once with the right email address. This is a ___.
Explanation: Asserting on interactions (was it called? with what?) = mock.
3. "___ the logger and assert it received the error message." Which word?
Explanation: When you assert on interactions, you mock. "Mock the logger and assert it was called" is standard usage.
4. Which sentence is correct?
Explanation: Returning fixed data without checking interactions = mock used as a stub (acceptable) — but strictly, that's a stub.
5. In informal conversation, developers often use "mock" to mean any test double. This is ___.
Explanation: "Mock" is widely used informally for any test double. In precise technical discussion, the distinction matters.
Frequently asked questions
What is a "test double"?
Gerard Meszaros's term for any object that stands in for a real dependency in tests. Subtypes include: dummy, stub, spy, mock, and fake.
What is a "fake"?
A working implementation of a dependency that is simpler than the real thing (e.g. an in-memory database instead of PostgreSQL). Has real logic, unlike a stub.
What is a "spy"?
A test double that wraps the real implementation and records calls — you get real behaviour plus interaction assertions.
What is a "dummy"?
An object passed around but never used — just satisfies a parameter requirement. No real logic, no pre-programmed responses.
Does it matter in practice?
In conversations and code reviews, yes. Saying "stub the payment service" vs "mock it" signals whether you'll verify the calls were made — different test design intentions.
What does "verify expectations" mean?
At the end of a test, calling mock.verify() or equivalent to confirm all expected interactions happened. If an expected call didn't occur, the test fails.
What is "over-mocking"?
Mocking too many dependencies, making tests that verify implementation details rather than behaviour. Over-mocked tests are brittle — they break when you refactor, not when you introduce bugs.
What is a "mock framework"?
A library that simplifies creating test doubles — e.g. Mockito (Java), jest.mock() (JS), unittest.mock (Python).
What does "mock it out" mean?
Informally: replace a real dependency with a test double for testing purposes. "Let's mock out the HTTP client" = use a fake/stub/mock instead of making real network calls.
What is the difference between mock and patch in Python?
"unittest.mock.patch" is a Python decorator/context manager that replaces an object during a test. "Mock" is the test double object it creates. You patch the location, which installs a Mock.