gpt4 book ai didi

elixir - (RuntimeError) 预期连接有响应

转载 作者:行者123 更新时间:2023-12-04 06:36:01 27 4
gpt4 key购买 nike

我是 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

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

我错过了什么?

最佳答案

在您的测试中,您使用 Plug.Test.conn/4获取 Plug.Conn struct 并将其作为参数传递给 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>

关于elixir - (RuntimeError) 预期连接有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30063819/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com