liveBook · Manning

Test double

As explained in The Art of Unit Testing 3e

A test double is a replacement for a real dependency that provides simplified behavior or enables observation during a test.

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)

Category

Pattern

Purpose

Uses

 

Test double

Generic name for stubs and mocks

I also use the term fake.

Stub

Dummy object

Used to specify the values to be used in tests when the only usage is as irrelevant arguments of SUT method calls

Send as a parameter to the entry point or as the arrange part of the AAA pattern.

 

Test stub

Used to verify logic independently when it depends on indirect inputs from other software components

Inject as a dependency, and configure it to return specific values or behavior into the SUT.

Mock

Test spy

Used to verify logic independently when it has indirect outputs to other software components

Override a single function on a real object, and verify that the fake function was called as expected.

 

Mock object

Used to verify logic independently when it depends on indirect outputs to other software components

Inject the fake as a dependency into the SUT, and verify that the fake was called as expected.

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

How The Art of Unit Testing 3e covers it

How deep it goes
Defines the umbrella term, distinguishes stubs from mocks, and lists injection techniques while deferring the five-pattern taxonomy.
Examples
Uses an in-memory database, injected functions, fake modules, constructors, and object parameters as replacement examples.
What it assumes
Assumes unit-test vocabulary such as system under test and dependencies while teaching stub-versus-mock terminology.
What you can do after
Apply test doubles by injecting controlled functions, objects, modules, or constructors and naming their roles precisely.
How it connects
Connects test doubles with stubs, mocks, fakes, indirect inputs, indirect outputs, exit points, and dependency injection.

Also explained in

Compare how 6 books explain test double →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.