Phoenix测试套件在运行测试时找不到列

Bit*_*ise 4 testing elixir phoenix-framework

我正在建立我的第一次凤凰测试.我只想断言屏幕上有内容.我正在ex_machina用于工厂,这可能是我的问题,但我不确定?这是我的代码.

表:

create table(:locations) do
  add :start,  :string
  add :finish, :string
end
Run Code Online (Sandbox Code Playgroud)

测试:

defmodule AwesomeLunch.LocationsControllerTest do
  use AwesomeLunch.ConnCase

  import AwesomeLunch.Factory

  test "shows start and finish for locations" do
    conn = conn()
    locations = insert(:locations)

    conn = get conn, locations_path(conn, :show, locations.id)

    assert html_response(conn, 200) =~ locations.start
  end
end
Run Code Online (Sandbox Code Playgroud)

控制器:

def show(conn, %{"id" => locations_id}) do
  locations = Repo.get!(Locations, locations_id)

  render conn, "show.html", locations: locations
end
Run Code Online (Sandbox Code Playgroud)

错误:

** (Postgrex.Error) ERROR (undefined_column): column "finish" of relation "locations" does not exist
 stacktrace:
   (ecto) lib/ecto/adapters/sql.ex:463: Ecto.Adapters.SQL.struct/6
   (ecto) lib/ecto/repo/schema.ex:397: Ecto.Repo.Schema.apply/4
   (ecto) lib/ecto/repo/schema.ex:193: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4
   (ecto) lib/ecto/repo/schema.ex:124: Ecto.Repo.Schema.insert!/4
   test/controllers/locations_controller_test.exs:8: (test)
Run Code Online (Sandbox Code Playgroud)

Ale*_*xHv 8

运行mix test运行迁移.

这可能发生了什么:

  1. 您创建了一个迁移文件mix ecto.gen.migration.
  2. 你跑了mix test.此时,所有迁移都已运行,包括新创建的迁移.此新迁移设置为在schema_migrations测试数据库的表中运行.
  3. 您使用添加表的说明编辑了迁移.
  4. mix test再次运行:在这一点上,mix没有看到新的迁移,因为新的迁移已经设置为run.您的新专栏从未添加过.

如果是这种情况,请运行MIX_ENV=test mix ecto.reset并重新运行所有迁移.(数据也将丢失,这对于测试数据库来说应该不是问题).

===

提示:您可以通过查看私有函数中的mix.exs文件来了解迁移的原因aliases:

  defp aliases do
    [...
     "test": ["ecto.create --quiet", "ecto.migrate", "test"]]
  end
Run Code Online (Sandbox Code Playgroud)