Could not lookup MyApp.Repo because it was not started or it does not exist

Background

I have a small ecto project that has some tests. This project is from the book “Designing Elixir Systems with OTP” and I am in the last part, testing the boundary.

The book was written with Elixir 1.8.* in mind, but I am using 1.9.* and I think this is what is causing the issue.

Code

This is my config.exs:

use Mix.Config

config :mastery_persistence,
  ecto_repos: [MasteryPersistence.Repo]

config :logger, level: :info

import_config "#{Mix.env()}.exs"

And here my test.exs config file:

use Mix.Config

config :mastery_persistence, MasteryPersistence.Repo,
  database: "mastery_test",
  hostname: "localhost",
  pool: Ecto.Adapters.SQL.Sandbox

As you can see there is nothing extraordinary. I guess one of the files where the issue could be is my application.ex:

defmodule MasteryPersistence.Application do
  @moduledoc false

  use Application

  def start(_type, _args) do
    children = [
      MasteryPersistence.Repo
    ]

    opts = [strategy: :one_for_one, name: MasteryPersistence.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

But this is mainly boilerplate code.

Error

The code is fairly simple, but when I run my test, it fails. Following is my test file:

defmodule MasteryPersistenceTest do
  use ExUnit.Case

  alias MasteryPersistenceTest.{Response, Repo}

  #########
  # Setup #
  #########

  setup do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
    response = %{
      quiz_title: :simple_addition,
      template_name: :single_digit_addition,
      to: "3 + 4",
      email: "student@example.com",
      answer: "7",
      correct: true,
      timestamp: DateTime.utc_now()
    }

    {:ok, %{response: response}}
  end

  #########
  # Tests #
  #########

  test "responses are recorded", %{response: response} do
    assert Repo.aggregate(Response, :count, :id) == 0
    assert :ok = MasteryPersistence.record_response(response)
    assert Repo.all(Response) |> Enum.map(fn r -> r.email end) == 
      [response.email]
  end

end

Aside from the setup, there isn’t really anything interesting here that could cause issues. The error is as follows:

1) test responses are recorded (MasteryPersistenceTest)
     test/mastery_persistence_test.exs:29
     ** (RuntimeError) could not lookup MasteryPersistenceTest.Repo because it was not started or it does not exist
     stacktrace:
       (ecto) lib/ecto/repo/registry.ex:18: Ecto.Repo.Registry.lookup/1
       (ecto) lib/ecto/adapter.ex:127: Ecto.Adapter.lookup_meta/1
       (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:486: Ecto.Adapters.SQL.Sandbox.lookup_meta!/1
       (ecto_sql) lib/ecto/adapters/sql/sandbox.ex:414: Ecto.Adapters.SQL.Sandbox.checkout/2
       test/mastery_persistence_test.exs:11: MasteryPersistenceTest.__ex_unit_setup_0/1
       test/mastery_persistence_test.exs:1: MasteryPersistenceTest.__ex_unit__/2

Research

I have tried to execute the original version from the book in elixir 1.8.* and it worked. The code files we have are basically identical.

I have also searched this forum for solution to this problem and I found one regarding Application.start but I am unsure if that even applies to this issue.

Question

I have concluded this is some configuration issue, I am missing something regarding 1.9.*. But what is it?
Do you think my conclusion makes sense?
What do you think the error is?

It looks like you’re trying to start MasteryPersistenceTest.Repo instead of MasteryPersistence.Repo

The alias in the test is wrong.