"Could not lookup Ecto repo" 测试期间出错
"Could not lookup Ecto repo" error during tests
我在尝试设置新的 ecto 存储库时遇到以下异常。目前仅用于测试(因为我只是设置它)。
** (RuntimeError) could not lookup Ecto repo Lipwig.AnnotatedUnit.Repo because it was not started or it does not exist
当我的测试使用 Lipwig.AnnotatedUnit.DataCase
时会发生这种情况。但是,当我使用 Lipwig.DataCase
(引用 Lipwig.Repo`)时,测试 运行 很好。
我在项目中有以下设置。
defmodule Lipwig.Repo do
use Ecto.Repo,
otp_app: :lipwig,
adapter: Ecto.Adapters.Postgres
end
defmodule Lipwig.AnnotatedUnit.Repo do
use Ecto.Repo,
otp_app: :lipwig,
adapter: Ecto.Adapters.Postgres
end
这是我的测试设置
defmodule Lipwig.DataCase do
use ExUnit.CaseTemplate
using do
quote do
alias Lipwig.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import Lipwig.DataCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Lipwig.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Lipwig.Repo, {:shared, self()})
end
:ok
end
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end
defmodule Lipwig.AnnotatedUnit.DataCase do
use ExUnit.CaseTemplate
using do
quote do
alias Lipwig.AnnotatedUnit.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import Lipwig.AnnotatedUnit.DataCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Lipwig.AnnotatedUnit.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Lipwig.AnnotatedUnit.Repo, {:shared, self()})
end
:ok
end
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end
我的 Lipwig.Repo
的另一个数据集几乎相同,只是引用了另一个 repo。
我的 config.exs
配置定义
config :lipwig,
ecto_repos: [Lipwig.Repo, Lipwig.AnnotatedUnit.Repo]
我的 test.exs
配置是
config :lipwig, Lipwig.Repo,
username: "postgres",
password: "postgres",
database: "lipwig_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox
config :lipwig, Lipwig.AnnotatedUnit.Repo,
username: "postgres",
password: "postgres",
database: "lipwig_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox
据我所知,配置看起来不错。正如错误所暗示的那样,我认为问题可能是因为您没有在您的应用程序中启动 Lipwig.AnnotatedUnit.Repo
。
在 /lib/lipwig_web
文件夹中查找 application.ex
文件,并确保 Lipwig.AnnotatedUnit.Repo
已添加到子数组中。
希望这能解决
我在尝试设置新的 ecto 存储库时遇到以下异常。目前仅用于测试(因为我只是设置它)。
** (RuntimeError) could not lookup Ecto repo Lipwig.AnnotatedUnit.Repo because it was not started or it does not exist
当我的测试使用 Lipwig.AnnotatedUnit.DataCase
时会发生这种情况。但是,当我使用 Lipwig.DataCase
(引用 Lipwig.Repo`)时,测试 运行 很好。
我在项目中有以下设置。
defmodule Lipwig.Repo do
use Ecto.Repo,
otp_app: :lipwig,
adapter: Ecto.Adapters.Postgres
end
defmodule Lipwig.AnnotatedUnit.Repo do
use Ecto.Repo,
otp_app: :lipwig,
adapter: Ecto.Adapters.Postgres
end
这是我的测试设置
defmodule Lipwig.DataCase do
use ExUnit.CaseTemplate
using do
quote do
alias Lipwig.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import Lipwig.DataCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Lipwig.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Lipwig.Repo, {:shared, self()})
end
:ok
end
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end
defmodule Lipwig.AnnotatedUnit.DataCase do
use ExUnit.CaseTemplate
using do
quote do
alias Lipwig.AnnotatedUnit.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import Lipwig.AnnotatedUnit.DataCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Lipwig.AnnotatedUnit.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Lipwig.AnnotatedUnit.Repo, {:shared, self()})
end
:ok
end
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
end
end
我的 Lipwig.Repo
的另一个数据集几乎相同,只是引用了另一个 repo。
我的 config.exs
配置定义
config :lipwig,
ecto_repos: [Lipwig.Repo, Lipwig.AnnotatedUnit.Repo]
我的 test.exs
配置是
config :lipwig, Lipwig.Repo,
username: "postgres",
password: "postgres",
database: "lipwig_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox
config :lipwig, Lipwig.AnnotatedUnit.Repo,
username: "postgres",
password: "postgres",
database: "lipwig_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox
据我所知,配置看起来不错。正如错误所暗示的那样,我认为问题可能是因为您没有在您的应用程序中启动 Lipwig.AnnotatedUnit.Repo
。
在 /lib/lipwig_web
文件夹中查找 application.ex
文件,并确保 Lipwig.AnnotatedUnit.Repo
已添加到子数组中。
希望这能解决