What is a test double?

Answer

A test double is any object used in tests to replace a real dependency. The term (coined by Gerard Meszaros) covers several types: Dummy: passed around but never actually used — just fills parameter lists. Stub: returns hardcoded responses to calls — "when called with X, return Y." No verification of calls. Fake: working implementation but simpler than production (e.g., in-memory database instead of real DB). Mock: pre-programmed with expectations about which calls it should receive — verifies interactions. If expected calls don't happen, the test fails. Spy: records calls and allows verification afterward — like a mock but more flexible (doesn't pre-define expectations). In practice, "mock" is often used loosely to mean any test double. Frameworks: Mockito (Java), Moq/NSubstitute (C#), unittest.mock (Python), Jest mocks, Sinon.js (JavaScript), Mockery/Prophecy (PHP).