Creating Stubs

Learn how to create stubs.

Creating stubs using Mox

The Mox.stub/3 takes three parameters:

  1. The mock module
  2. The name of the function to be stubbed
  3. An anonymous function of the same arity as the function to be stubbed

The anonymous function is what will be executed when the stubbed function is invoked on the mock module. Usually, stub/3 is used inside single tests.

We can rewrite the test for our weather API example so that instead of using SoggyWaffle.FakeWeatherAPI, it uses an anonymous function that does the same thing that SoggyWaffle.FakeWeatherAPI.get_forecast/1 does:

Take a look at the updated SoggyWaffleTest.

Get hands-on with 1200+ tech skills courses.