Test Doubles: Stubs, Mocks, and Fakes

Get familiar with the three kinds of doubles: fakes, stubs, and mocks.

Definitions

Until now, we’ve just used the term external dependency double to refer to something that doubles as an external dependency. However, we can be more precise and define three kinds of doubles: fakes, stubs, and mocks.

Fake

A fake is just something that acts as the external dependency without calling out to the external dependency. We already saw an example of a fake with SoggyWaffle.FakeWeatherAPI in Dependency Doubles.

Stub

A stub is a double where function implementations can be replaced on demand to do something in isolation. For example, we can replace a function with a double that does one thing in one test and something else in another. Stubs can provide a different behavior of a given function based on the need of the test. Stubs are most useful when combined with mocks.

Mock

A mock is essentially a stub that can be used to assert how and how often the double is called.

Get hands-on with 1400+ tech skills courses.