Last active
June 26, 2020 14:49
-
-
Save bangn/b91bec2218b86f9ed1ec8bd3944496f6 to your computer and use it in GitHub Desktop.
Set up Ecto to run test in Sanbox mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{: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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ;)