Elixir / Phoenix - 单独运行文件时测试通过,但使用混合测试时失败

时间:2017-12-06 15:07:16

标签: elixir phoenix-framework

我遇到的问题是运行像这样的单个测试文件会传递:

mix test test/app_web/controllers/page_controller_test.exs

但运行整个测试套件会失败: mix test

 1) test GET / (AppWeb.PageControllerTest)
 test/app_web/controllers/page_controller_test.exs:4
 ** (RuntimeError) expected response with status 200, got: 302, with body:
 <html><body>You are being <a href="http://localhost:3000">redirected</a>.</body></html>
 code: assert html_response(conn, 200) =~ "Welcome to Phoenix!"
 stacktrace:
   (phoenix) lib/phoenix/test/conn_test.ex:362: Phoenix.ConnTest.response/2
   (phoenix) lib/phoenix/test/conn_test.ex:376: Phoenix.ConnTest.html_response/2
   test/app_web/controllers/page_controller_test.exs:6: (test)

我不太确定发生了什么,但我的路由器插件检查是否为所有传入的请求设置了特定的cookie。

所以我编辑了setup中的默认test/support/conn_case.ex,将该Cookie添加到conn。像这样(我的应用程序不需要数据库)。

setup do
  token = Support.Token.generate_token()

  conn =
    ConnTest.build_conn()
    |> ConnTest.put_req_cookie("session_token", token)
    |> Plug.Conn.fetch_cookies()

  {:ok, conn: conn}
end

有谁知道发生了什么事?

1 个答案:

答案 0 :(得分:-1)

在错误日志中,它显示302。这意味着它重新指向其他URI。这就是为什么它显示你的HTML文件的内容。你的重新定位有些不对劲。