mccraigmccraig

mccraigmccraig

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…

Showing Posts 1 to 2

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:

mccraigmccraig

mccraigmccraig OP

ha!

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

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New

Other Trending Topics Top

akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews