2

I am trying to set up an app under umbrella that will handle the business logic. It uses Ecto for interacting with the database. I’m having problems with setting up SQL.Sandbox for testing. Whenever I run test, I get this error:

$ MIX_ENV=test mix test
** (exit) exited in: GenServer.call(Domain.Repo.Pool, :checkin, 5000)
  ** (EXIT) no process
  (elixir) lib/gen_server.ex:596: GenServer.call/3
  lib/ecto/adapters/sql/sandbox.ex:422: Ecto.Adapters.SQL.Sandbox.mode/2
  (elixir) lib/code.ex:363: Code.require_file/2
  (elixir) lib/enum.ex:651: Enum."-each/2-lists^foreach/1-0-"/2
  (elixir) lib/enum.ex:651: Enum.each/2

My config.exs looks like this:

use Mix.Config

config :domain,
  ecto_repos: [Domain.Repo]

config :domain, Domain.Repo,
  adapter: Ecto.Adapters.Postgres,
  pool: Ecto.Adapters.SQL.Sandbox,
  username: "postgres",
  password: "postgres",
  database: "app_test"

My test_helper.exs is:

ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Domain.Repo, :manual)
Maciej Szlosarczyk
  • 789
  • 2
  • 7
  • 21
  • I believe you need to add the following to the `setup` block of the test modules (Phoenix does this): `:ok = Ecto.Adapters.SQL.Sandbox.checkout(Domain.Repo)`. Could you try that out? – Dogbert Sep 18 '16 at 06:12
  • I just added the following to the tests, still the same behaviour: `setup do :ok = Ecto.Adapters.SQL.Sandbox.checkout(Domain.Repo) end` – Maciej Szlosarczyk Sep 18 '16 at 06:20
  • 1
    Ah, I misread the error. You need to start `Domain.Repo` before the tests. If this package has an `Application`, try adding `supervisor(Domain.Repo, [])` to its children list, otherwise try adding `Domain.Repo.start_link()` after `ExUnit.start()`. – Dogbert Sep 18 '16 at 07:52
  • That did the trick, thanks! – Maciej Szlosarczyk Sep 18 '16 at 09:17

1 Answers1

4

(EXIT) no process in GenServer.call means that the server you tried to send a call request to is not currently alive. You'll have to ensure Domain.Repo is running before you call Ecto.Adapters.SQL.Sandbox.mode(Domain.Repo, :manual).

The most common way is to add Domain.Repo as a Supervisor to the Application's supervision tree. To do this, add the following to the children list in Domain.start/2:

children = [
  ...,
  supervisor(Domain.Repo, []) # add this
]

If, for some reason, you only want to start the Repo in your tests, you can also add the following before calling Ecto.Adapters.SQL.Sandbox.mode(Domain.Repo, :manual) in test/test_helper.exs:

Domain.Repo.start_link()
Dogbert
  • 212,659
  • 41
  • 396
  • 397