种子伞项目(Ecto / Postgre)

时间:2018-04-21 22:44:55

标签: elixir ecto

我目前有一个包含数据库服务的伞形项目。我想在运行mix test命令时自动播种此服务,但我不能。

我尝试将以下代码放入apps/db_service/mix.ex

defp aliases do
    [
        "ecto.setup": ["ecto.create --quiet", "ecto.migrate", "run priv/repo/seeds.exs"],
        "ecto.reset": ["ecto.drop", "ecto.setup"],
        "test": ["ecto.reset", "test"]
    ]
end

但是如果我在根目录上运行mix test我收到错误:** (Mix) The database for DatabaseService.Repo couldn't be dropped: ERROR 55006 (object_in_use)(命令正在服务目录中运行)

所以我试着加入root/mix.ex

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

并进入apps/db_service/mix.ex

defp aliases do
    [
        "test": ["run priv/repo/seeds.exs", "test"]
    ]
end

但是现在,我收到以下错误:** (Mix) No such file: priv/repo/seeds.exs

如何为种子测试做一下,让mix test命令在根目录和服务文件夹中工作?

2 个答案:

答案 0 :(得分:1)

这是一个很晚的答案,但它在这里:

defp aliases do
  "test.seed": ["run #{Path.join(__DIR__, "priv/repo/seeds.exs")}"],
  test: ["test.seed", "test"]
end

取自Github上的this response

答案 1 :(得分:-1)

当您必须执行mix test时,请使用MIX_ENV=test mix test。为了在测试数据库中运行任务,我们需要指定环境,就像创建测试环境单独数据库一样。

因此使用MIX_ENV=test然后命令,以便它在测试数据库上运行任务。

相关问题