CheckOps home

Tools directory

M

CheckOps

Mox

Mox is an Apache-2.0 Elixir library from Dashbit for defining concurrent, behaviour-based mocks that follow the "mocks as explicit contracts" principles and work with async tests.

Visit website

Latest public signals

Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.

Visit Website

Introduction

Mox is a library for defining concurrent mocks in Elixir, created by José Valim’s company Dashbit. It follows the principles from the article “Mocks and explicit contracts”: no ad-hoc mocks (mocks are only created from behaviours), no dynamic module generation during tests, concurrency support so tests using the same mock can still run with async: true, and reliance on pattern matching and function clauses for asserting inputs rather than complex expectation rules.

You define a mock for a behaviour with Mox.defmock(MyApp.MockWeather, for: MyApp.WeatherAPI) in test_helper.exs, configure your application to use the mock in tests, then set expectations with expect(MockWeather, :temp, fn {lat, lon} -> {:ok, 30} end) or stubs with stub/3. verify_on_exit! checks that expectations were met. Because expectations are scoped to the test process (with explicit allowances or global mode for other processes), concurrent tests do not interfere. The goal, per the README, is to help you define contracts between parts of your application.

The source code is at dashbitco/mox on GitHub under the Apache-2.0 licence, with about 1,400 stars; the most recent commit was on 7 November 2026.

Features

  1. Behaviour-based: Mocks only for explicit contracts.
  2. Async-safe: Process-scoped expectations work with async: true.
  3. Pattern matching: Assert inputs with function clauses.
  4. Verification: verify_on_exit! ensures expectations ran.

Pros

  1. Free to use: Open source under the Apache-2.0 licence, so the core tool costs nothing to use.
  2. Actively maintained: The repository had commits as recent as 7 November 2026.

Cons

  1. Requires behaviours: Code must depend on behaviours and configurable modules.
  2. Process allowances: Mocks used in other processes need explicit allowances.
# test/test_helper.exs
Mox.defmock(Shop.MockPayments, for: Shop.Payments)
Application.put_env(:shop, :payments, Shop.MockPayments)

# test/checkout_test.exs
defmodule Shop.CheckoutTest do
  use ExUnit.Case, async: true
  import Mox
  setup :verify_on_exit!

  test "charges the total" do
    expect(Shop.MockPayments, :charge, fn 100 -> {:ok, "tx-1"} end)
    assert {:ok, "tx-1"} = Shop.Checkout.pay(100)
  end
end

Resources:

Pricing

Mox is free and open source under the Apache-2.0 licence.

  1. Elixir teams mocking external services behind behaviours.
  2. Projects with async test suites.

Conclusion

Mox is the idiomatic Elixir mocking library, encouraging explicit contracts while keeping tests concurrent.

Alternatives

Other tools on CheckOps that cover similar ground. Open a page, or compare one with Mox side by side.