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 ___.

2. You replace the email service with an object and assert it was called once with the right email address. This is a ___.

3. "___ the logger and assert it received the error message." Which word?

4. Which sentence is correct?

5. In informal conversation, developers often use "mock" to mean any test double. This is ___.

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.