8

我是 Phoenix Framework 的新用户,我正在尝试设置一个简单的 HTTP POST 服务,该服务对传入数据执行计算并返回结果,但出现以下错误:

** (RuntimeError) expected connection to have a response but no response was set/sent
 stacktrace:
   (phoenix) lib/phoenix/conn_test.ex:311: Phoenix.ConnTest.response/2
   (phoenix) lib/phoenix/conn_test.ex:366: Phoenix.ConnTest.json_response/2
   test/controllers/translation_controller_test.exs:20

我的测试用例:

test "simple POST" do
  post conn(), "/api/v1/foo", %{"request" => "bar"}
  IO.inspect body = json_response(conn, 200)
end

我的路由器定义:

scope "/api", MyWeb do
  pipe_through :api

  post "/v1/foo", TranslationController, :transform
end

我的控制器:

def transform(conn, params) do
  doc = Map.get(params, "request")
  json conn, %{"response" => "grill"}
end

我错过了什么?

4

1 回答 1

12

在您的测试中,您使用Plug.Test.conn/4获取Plug.Conn结构并将其作为参数传递给post. 但是,您不会将结果存储在名为conn.

这意味着第二次使用conn, 在检查时json_response实际上是第二次调用Plug.Test.conn/4.

试试这个:

test "simple POST" do
  conn = post conn(), "/api/v1/foo", %{"request" => "bar"}
  assert json_response(conn, 200) == <whatever the expected JSON should be>
于 2015-05-05T22:14:27.550 回答