MEPX
Chapter 4 of 10All chapters

Chapter 4 of 10

Fakes, stubs and mocks

Standing in for the awkward parts.

The vocabulary

A stub returns canned answers. A fake is a working simplified implementation, such as an in-memory store. A mock records calls so the test can assert on them.

  • Prefer fakes and stubs; they check outcomes rather than implementation.
  • Mocking everything produces tests that break on every refactor and catch nothing.

Where the seam goes

Code that creates its own dependencies cannot be tested without them. Passing them in is what makes substitution possible, which is why testability and good design keep pointing the same way.