Creating mocks with Mox

As mentioned, we usually try to avoid mocks in Elixir, and especially dynamic mocks (also known as ad-hoc mocks). They're called this because they're usually created dynamically within each test, just to verify that a certain expectation was met on that test. Besides changing the global behavior and not allowing us to run our tests concurrently, this type of mocks have another disadvantage: they have no connection to the real implementation they're standing in for, which makes it possible for the mock and the real implementation to drift away, possibly resulting in having all the tests passing while your application is broken in production.

For all these reasons, we'll be creating mocks with the Mox library, created ...

Get Mastering Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.