在控制器测试中使用响应数据

时间:2018-09-24 09:05:29

标签: testing controller elixir phoenix-framework

我进行了以下测试,其中使用了先前请求中的数据:

test "renders user when data is valid", %{conn: conn} do
  conn = post conn, user_path(conn, :create), user: @create_attrs
  assert %{"jwt" => jwt} = json_response(conn, 201)["data"]

  conn = get (conn |> put_req_header("Authorization", "Bearer #{jwt}")), user_path(conn, :show)

  assert %{"id" => _, "email" => "some email", "password_hash" => "some password_hash"} = json_response(conn, 201)["data"]

end

但是此测试失败并显示:

** (Plug.Conn.AlreadySentError) the response was already sent

如何在这里重用conn

1 个答案:

答案 0 :(得分:1)

使用原始的connGET请求中获得期望的结果(我将{{1}的conn替换为conn1

POST