Skip to content

Instantly share code, notes, and snippets.

Last active Jun 26, 2020
What would you like to do?
Set up Ecto to run test in Sanbox mode
defmodule MyApp.Test do
use ExUnit.Case, async: true
use Plug.Test
setup do
# Checkout repo manually.
# TODO: figure out how to do it automatically.
:ok = Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo)
# Test your business here.
use Mix.Config
adapter: Ecto.Adapters.Postgres,
pool: Ecto.Adapters.SQL.Sandbox, # This is needed to run in Sandbox mode.
database: "database-test",
hostname: "localhost"
{:ok, _} = Application.ensure_all_started(:ex_machina)
Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, :manual) # This is needed to run in Sanbox mode.
Copy link

GesJeremie commented Oct 1, 2016

You shouldn't do that. Everything is already executed in a transaction and cleaned by itself. I suppose you are running a seed task before to run your tests, in that case ok. Still you could just check with some conditions if the database is already seeded in your seed file ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment