DoubleDown - DB-free Ecto tests with ExMachina and stateful fakes

Tired of waiting for slow Ecto sandbox tests ?

At work, we have 45,000 tests - taking 12 minutes on a fast laptop and ~45 minutes of elapsed CI time. No-one runs the full test suite locally anymore.

DoubleDown is a test-double library that offers an alternative. It targets tests that are using the database as a convenience, to get test data into the right place at the right time. Its centrepiece is Repo.InMemory — a stateful Ecto.Repo fake that is powerful enough to run ExMachina factory tests unchanged, but without
a database. In a typical project, tests that only use the DB for data-fixturing run hundreds of times faster.

Create a Repo facade that doubles the built-in DoubleDown.Repo contract:

defmodule MyApp.Repo do
  use DoubleDown.ContractFacade, contract: DoubleDown.Repo, otp_app: :my_app
end

With that in place, install the in-memory fake in your test setup:

setup do
  DoubleDown.Double.fallback(DoubleDown.Repo, DoubleDown.Repo.InMemory)
  :ok
end

test "factory-inserted records are readable" do
  insert(:user, name: "Alice")
  insert(:user, name: "Bob")

  assert [_, _] = MyApp.Repo.all(User)
  assert %User{name: "Alice"} = MyApp.Repo.get_by(User, name: "Alice")
end

That’s it. There’s no Sandbox, no Ecto.Adapters.SQL, and no database. Repo.InMemory understands just enough of structs, schemas, changesets, associations, transactions, and Ecto.Multi to make common operations work. If you are testing queries or constraints, you should carry on using the Ecto sandbox - but early experience has been that ~75% of unit tests don’t need the sandbox.

What it handles

Writes, PK reads, aggregates, preloads, insert_all/update_all/delete_all, Ecto.Multi transactions with full rollback — all in memory. FK backfill on insert (point ExMachina at your Repo and insert(:child, parent: parent)
just works). Ecto.Query operations, stream, and query/query! delegate to an optional fallback function — the in-memory store handles the simple stuff, you handle the complex queries with a one-line stub.

Beyond the Repo fake

DoubleDown is a general-purpose test-double library in the Mox lineage.
It supports three ways to add test boundaries to your system:

Facade Contract Best for
ContractFacade defcallback (explicit, typed) New code you control
BehaviourFacade existing @behaviour Third-party or legacy behaviours
DynamicFacade implicit (module’s public API) Adding boundaries without touching source

All three share the same dispatch mechanism and the same test API
(expect, stub, fake, fallback, verify!), whether you’re intercepting
an Ecto Repo call or a domain service.

How it relates to Mox

DoubleDown builds on the Mox pattern — behaviours, dispatch facades,
compile-time dispatch resolution. The key differences:

  • Stateful fakes. Handlers can carry state across calls — which is how the Repo fake works. No equivalent in Mox.
  • DynamicFacade. Add a boundary by calling setup(Module) in test_helper.exs — no contract module, no config wiring. More like Mimic, but with the full Double API.
  • Cross-contract state. Fakes can access each other’s state at dispatch time, enabling multi-contract orchestration tests.
  • Defer for re-entrant calls. Handle the case where a test double needs to call another contract mid-handler without deadlocking.

Getting started

Install {:double_down, "~> 0.62"} from Hex and create a Repo facade
in two lines. The README and
docs have the full story.

Thoughts

Have you run into the slow Ecto sandbox issue ? What did you do ? Hit me up
with any questions…

3 Likes

Nice!
What a timing - two posts about long test suite within 1h :smiley:
I’ve hit the same hurdle but I utilized test partitions to bring CI time down
I will definetely check doubledown lib :ok_hand:

ha!

and two quite different approaches - I will check out Check!