Pass a list of integers as a field in a post body in a Controller test

I am trying to pass in a list of integers to as the body to a post call in a Controller test in Phoenix like so:

   params = %{
      int_list: [3, 6, 12, 24],
   }

    conn = post(conn, Routes.my_path(conn, :update, params))

    assert json_response(conn, 200) ==
             %{
               "data" => %{
                 "list" => [3, 6, 12, 24]
               }
             }

But this gives an error because the list has become a list of strings and when I inspect the params passed to this route in the controller the list becomes:

["3", "6", "12", "24"]

Do I have to de-serialize this myself or am I not passing the parameters in correctly in my test? Thank you!

Depends on what the encoding of your params is, but the default of url encoded will make everything be strings. Json encoded bodies hold more type information, which would give you integer values for params. No matter the input encoding I always suggest running params through a changeset to convert input to a known and validated format.

1 Like

I figured it out I just needed to pass the params like this:

    conn = post(conn, Routes.my_path(conn, :update), params)

You are right @LostKobrakai! The way I was passing the params they were getting url encoded. I wanted to pass them as the body… Now my test is passing!