A test double replaces a non-production dependency during a test. It can provide predictable input, simplify an unreliable component, or let a test observe an outgoing call. “Test double” is the broad term; stub, mock, and fake describe more specific uses. section 3.1 section 3.8
Choose the name according to what the replacement does:
- Use a stub when the system under test needs controlled input or behavior from an incoming dependency. section 3.1 section 3.8
- Use a mock when the test verifies an outgoing call or other indirect output at an exit point. section 3.1 section 3.8
- Use fake as a general shorthand for something non-real, such as an in-memory implementation replacing a production database. section 3.1
Inject a predictable dependency
A function can accept a dependency as a parameter: `function loadUser(getUser) { return getUser(); }`. A test can pass `() => ({ name: "Ada" })`, making the incoming result reliable without using the real dependency. The same replacement could serve as a stub when supplying input, or as a mock when the test checks whether it was called. section 3.8 section 3.1
Table 3.1 Clarifying terminology around stubs and mocks (view table figure)
Use the terminology table to compare dummy objects, test stubs, test spies, and mock objects by purpose and placement. section 3.1
Prefer precise terms because calling every replacement a mock obscures whether a test controls input or verifies output. Inject doubles through function parameters, factories, module replacement, constructors, object parameters, or shared interfaces. Module replacement can be fragile when the replaced API changes. section 3.1 section 3.8





