这应该很简单,但我只是不知道我应该做什么。我有一个这样的 Controller 操作:
def submit(conn, %{"id" => id, "_json" => results}) do
...
end
(_json
键由 Plug.Parsers.JSON
在传入的 JSON 数组上自动生成)
我想测试一下。但是,如果我写
post(conn, experiment_path(conn, :submit, %{})),
或者类似的东西
post(
conn,
experiment_path(conn, :submit, %{
"id" => "123",
"_json" => [%{"a" => 1, "b" => 2}, %{"a" => 3, "b" => 4}]
})
)
我明白了
** (ArgumentError) maps cannot be converted to_param. A struct was expected, got: %{:_json => %{}, "id" => "123"}
烦人的是,我不确定这里究竟需要什么类型的结构。我搜索了一下,但大多数在线资源似乎都在谈论如何测试发送 JSON 响应,而不是如何接收 JSON 数据。
如何正确指定 Controller 接收到的JSON数据并编写测试?
最佳答案
正如@AlekseiMatiushkin 在评论中提到的,传入的 JSON 应该是 post
的参数,而不是 experiment_path
的参数。
这有效:
post(
conn,
experiment_path(conn, :submit, "123"),
%{
"_json" => [%{"a" => 1, "b" => 2}, %{"a" => 3, "b" => 4}]
}
)
关于json - 如何测试期望通过 POST 请求接收 JSON 数组的 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52793768/