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 value that is needed for the test.
Spy
A stub that needs the be called by the method under test in a specific way. Spying is useful when you are testing things across boundaries. Note. The test gets coupled to the implementation. If you don’t cross the boundary use a stub.
Mock
A spy that knows what is needed to pass (the logic is inside the mock instead of the test method)
Fake
For Unit tests fakes are unnecessarily complicated. Don’t use them for unit testing. Sometime simple fakes can be useful for integration tests. Note. You need tests to validate the fake is working correctly.
References
- The Little Mocker - CleanCoder
- Method stub - Wikipedia
- Mock object - Wikipedia