Search⌘ K
AI Features

Creating Mocks

Explore how to create mocks with Mox in Elixir to assert specific function call counts and behaviors. Understand how to set expectations, use anonymous functions for verification, and apply verification methods like verify! and verify_on_exit! to ensure your integration tests accurately simulate external dependencies.

We'll cover the following...

Mocks

A mock works the same way a stub does, but it lets us assert that the mocked function is called a specific number of times. In our example, we assert that rain?/2 calls get_forecast/1 only once.

Semantically, mocks are also used when we have some general expectations about how the mock module should be called. To create mock functions, Mox provides Mox.expect/4, which takes the name of the mock module, the function’s name, the number of times the mocked function can be invoked, and an anonymous ...