Skip to content

Instantly share code, notes, and snippets.

@bangn
Last active June 26, 2020 14:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bangn/b91bec2218b86f9ed1ec8bd3944496f6 to your computer and use it in GitHub Desktop.
Save bangn/b91bec2218b86f9ed1ec8bd3944496f6 to your computer and use it in GitHub Desktop.
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)
end
# Test your business here.
end
use Mix.Config
config(
:my_app,
MyApp.Repo,
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.
ExUnit.start()
@GesJeremie
Copy link

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