Search⌘ K

Test Doubles: Stubs, Mocks, and Fakes

Explore how to employ test doubles including fakes stubs and mocks to simulate external dependencies in Elixir tests. Understand how to leverage the Mox library for defining dynamic test doubles that help isolate components and verify interactions, enhancing your integration and end-to-end testing skills.

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 ...