Test Ecto queries and error "could not find Ecto repos in any of the apps: [:my_app]"

I tried following the article “Testing with Ecto”:
https://hexdocs.pm/ecto/testing-with-ecto.html

However I have a series of warnings and errors; first is:

warning: could not find Ecto repos in any of the apps: [:my_app].

However, I have a config/test.exs file with the following content:

use Mix.Config

config :my_app, MyApp.Repo,
  username: "postgres",
  password: "postgres",
  database: "my_app_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox

I created the MyApp.RepoCase and use it in the test file (use MyApp.RepoCase) as mentioned in the article, and appended this line to test_helper.exs:

Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, :manual)

What is missing?

It ends in a runtime error:

** (RuntimeError) could not lookup Ecto repo MyApp.Repo because it was not started or it does not exist

Note that this is a library, so I do not want to make an application that will start when a user depend on the library.

Have you created the database for tests?

For convenience reasons, you can also define aliases to automatically set up your database at the execution of your tests. Change the following content in your mix.exs .

def project do
    [app: :my_app,

     ...

     aliases: aliases()]
  end

  defp aliases do
    [ ...
     "test": ["ecto.create --quiet", "ecto.migrate", "test"]
    ]
  end

If you ant to use migration Ecto commands then you need to define:

config :my_app, ecto_repos: [MyApp.Repo]

Additionally have you started MyApp.Repo within your application supervisor? Because second error says it is not started yet.

1 Like

This is a library, I don’t want to have a supervisor. I’ve seen other libraries testing Ecto queries without the application.ex; but I do not understand how.

You need to start the repo (supervised or not) when running your tests.

7 Likes