我是菲尼克斯框架的新用户,我正在尝试设置一个简单的HTTP服务,该服务对传入的数据执行计算并返回结果,但我得到了以下错误:
** (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我遗漏了什么?
发布于 2015-05-05 22:14:27
在您的测试中,您使用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>https://stackoverflow.com/questions/30063819
复制相似问题