Definition

Test double

What is a test double, and how do stubs, fakes and mocks differ?

A test double stands in for a dependency during a test. A stub supplies a prepared response, a fake runs a lightweight implementation, and a mock checks expected interactions. Each lets the real code under test run against a controlled collaborator.

Which double fits the question?

Use a stub to make a dependency return a particular result. Use a fake when later steps need to read what an earlier step wrote. Check a mock’s interactions when sending a command is itself an observable outcome. Keep a separate check for the real dependency’s behaviour at the integration boundary.

Worked example

Testing a reminder service

A stub clock puts a reminder exactly at its due time. An in-memory fake stores pending reminders. A mock mail sender records the expected recipient and message. The scheduling code still decides which reminder is due, so a mistake in that decision can fail the test.

Try it on your pull request

Choose a change in your repository and tell Cue April what should happen. Review the proposed checks before they run.

Try Cue April on your repository