Creating Mocks

Learn how to create mocks.

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 function (similar to stub/3).

Get hands-on with 1200+ tech skills courses.