Maciej Szlosarczyk
Maciej Szlosarczyk

Reputation: 809

Testing Ecto 2.0 in an umbrella app

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)

Upvotes: 2

Views: 937

Answers (1)

Dogbert
Dogbert

Reputation: 222138

(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()

Upvotes: 4

Related Questions