mccraigmccraig

mccraigmccraig

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…

First Post!

funboy

funboy

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:

Where Next?

Popular in Announcing Top

deadtrickster
I’ve just released stable versions of my Prometheus Elixir libs: Elixir client [docs]; Ecto collector [docs]; Plugs instrumenter/Export...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
sabiwara
Dune is a sandbox for Elixir and aims to safely evaluate user-provided code. You can try it out using this basic Elixir playground made ...
New
josevalim
Hello everyone, We have just released NimbleCSV which is a small and fast CSV parsing library for Elixir. It allows developers to define...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 14085 106
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement