Test Doubles
Types of test doubles
Dummy
An object that we need to pass to the method under test but that is not relevant to the method under test or the test itself.
Stub
A dummy that returns a specific values, or throws exceptions needed for the test.
Spy
A stub that records how it is used by the method under test. Which methods were called, with what arguments. The test’s assertions then verify these recorded interactions. Spies are especially useful for verifying behavior across boundaries.
Mock
A spy that contains logic to decide whether the test passes or fails. The verification is done by the mock itself instead of in the test’s assertions.
Fake
A lightweight implementation of an interface that contains some working logic. For example an in-memory database or an in-memory cache store. Fakes can be complicated. Try to avoid them whenever possible.
References
- The Little Mocker - CleanCoder
- Method stub - Wikipedia
- Mock object - Wikipedia
- We Need to Stop Calling Everything a Mock! - YouTube: Modern Software Engineering