22.4 · Fakes, mocks and the inputs that make tests random

Hard-to-test code is usually code that reaches out for its dependencies and for “now”. Testability is a design property: make the edges explicit and the tests become small and boring.

Mental model. A test controls every input to the code under test: collaborators, time, randomness, locale, files. Anything it cannot control becomes a flake.

Test doubles

Dummies, stubs, fakes, spies and mocks, and when each one fits.

  1. What is the difference between dummies, stubs, fakes, spies and mocks, and how do you decide which to use? 22.4.1 · L3
    These are all test doubles: stand-ins for a real collaborator, made possible by depending on a protocol (or a closure) instead of a concrete type, with the…

Determinism

Time, randomness, locale, time zone and the file system.

  1. What makes a test non-deterministic, and how do you control time zones, locales, randomness, UUIDs and the file system? 22.4.2 · L3
    A test is deterministic if the same code and inputs give the same result on every machine and every run. It stops being so when it silently depends on ambient…

More in Testing