A Mock is a test double in Gerard Meszaros' taxonomy from xUnit Test Patterns (2007) that is pre-programmed with expectations about which calls the system under test should make. Its built-in assertions fail the test immediately on a wrong method, wrong arguments or wrong order. Unlike a Stub it actively verifies behaviour, and unlike a Spy it enforces expectations during execution, which suits interaction-based London School TDD.

Test Double: Mock (Meszaros)

Details

Core Concepts:

Purpose

Pre-programmed with expectations about which calls should be made. Verifies that specific interactions occurred — the test fails if the expected interactions don’t happen.

Behavior

Has built-in assertions. If the system under test calls the wrong method, with wrong arguments, or in the wrong order, the Mock fails the test immediately.

Distinction from Stub

A Stub provides data passively. A Mock actively verifies behavior.

Distinction from Spy

A Spy records calls for post-hoc assertion. A Mock enforces expectations during execution.

Key Proponent

Gerard Meszaros ("xUnit Test Patterns", 2007)

When to Use:

  • Tell an LLM: "Create a Mock test double that verifies these interactions occur"

  • Interaction-based testing (London School TDD)

  • When the behavior matters more than the result

  • Verifying that a notification was sent, a log was written, or a service was called correctly