I have a simple data:
{
"name" : "example task",
"description":"example description",
"type" : "task",
"details" : {
"__type__":"task",
"amount":20.00,
},
"publish" : true
}
When I test my API with postman, everything works. But when I write a unit test for controller, I got this error:
1) test create_task: assign task (OkBackendWeb.TaskControllerTest)
test/ok_backend_web/controllers/task_controller_test.exs:87
** (Protocol.UndefinedError) protocol Phoenix.Param not implemented for 20.0 of type Float. This protocol is implemented for the following type(s): Any, Atom, BitString, Integer, Map
code: conn = post(conn, Routes.item_path(conn, :create_all, @create_attrs))
stacktrace:
(phoenix 1.5.13) lib/phoenix/param.ex:121: Phoenix.Param.Any.to_param/1
(plug 1.12.1) lib/plug/conn/query.ex:275: Plug.Conn.Query.encode_value/2
(plug 1.12.1) lib/plug/conn/query.ex:246: Plug.Conn.Query.encode_pair/3
(plug 1.12.1) lib/plug/conn/query.ex:262: anonymous fn/3 in Plug.Conn.Query.encode_kv/3
(elixir 1.13.1) lib/enum.ex:1213: anonymous fn/3 in Enum.flat_map/2
(stdlib 3.14) maps.erl:233: :maps.fold_1/3
(elixir 1.13.1) lib/enum.ex:2408: Enum.flat_map/2
(plug 1.12.1) lib/plug/conn/query.ex:266: Plug.Conn.Query.encode_kv/3
(plug 1.12.1) lib/plug/conn/query.ex:262: anonymous fn/3 in Plug.Conn.Query.encode_kv/3
(elixir 1.13.1) lib/enum.ex:4086: Enum.flat_map_list/2
(elixir 1.13.1) lib/enum.ex:4087: Enum.flat_map_list/2
(plug 1.12.1) lib/plug/conn/query.ex:266: Plug.Conn.Query.encode_kv/3
(plug 1.12.1) lib/plug/conn/query.ex:204: Plug.Conn.Query.encode/2
(ok_backend 0.1.0) lib/ok_backend_web/router.ex:1: OkBackendWeb.Router.Helpers.segments/5
(ok_backend 0.1.0) lib/ok_backend_web/router.ex:1: OkBackendWeb.Router.Helpers.item_path/3
test/ok_backend_web/controllers/task_controller_test.exs:91: (test)
this is my test exs:
defmodule ExampleWeb.TaskControllerTest do
use ExampleWeb.ConnCase
alias Example.Users
alias Example.Items
@create_attrs %{
"name" => "milk with cow",
"description"=> "cow are cute.",
"type" => "task",
"details" => %{
"amount"=> 20.00,
},
"publish" => true
}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "create_task:" do
test "create task", %{conn: conn} do
%{ token: [ token ] } = get_poster_token(conn)
conn = post(conn, Routes.item_path(conn, :create_all, @create_attrs))
assert json_response(conn, 200)["status"] == 1
end
end
defp get_poster_token(conn) do
conn = post(conn, Routes.passwordless_path(conn, :user_auth, @user_poster))
token = get_resp_header(conn, "authorization")
%{token: token}
end
How can I resolve this?
CodePudding user response:
You're passing the POST data as an argument to the routes function. It should be a separate argument to post/3
:
post(conn, Routes.item_path(conn, :create_all), item: @create_attrs)