Hi,
I’m constantly being blocked by maps’ different construct %{:atom => “value”} vs. %{“string” => “value”} vs %{name: “value”}
It does not appears to be consistent enough and in my case are often the cause of all “blockage” I have.
I understand them, I’m a long time Elm dev/fan and used to FP, but I don’t know why Elixir’s maps are so weird to me.
For instance, I have this simple test that I’m trying to understand the error:
test "renders created category when valid", %{conn: conn} do
conn = post(conn, Routes.category_path(conn, :create), %{name: "cat name here"})
assert %{"name" => "cat name here"}
end
In my controller:
def create(conn, %{} = payload) do
{account_id, _user_id} = conn.assigns.current_user
payload = Map.put(payload, :account_id, account_id)
cat = KB.create_category(payload)
render(conn, "short.html", %{category: cat})
end
The error I’m getting:
1) test creates category renders created category when valid (Parle.CategoryControllerTest)
test/parle_web/controllers/category_controller_test.exs:19
** (Ecto.CastError) expected params to be a map with atoms or string keys, got a map with mixed keys: %{:account_id => "14ab1382-fe6a-4fd4-af6c-58c6a86c995e", "name" => "cat name here"}
code: conn = post(conn, Routes.category_path(conn, :create), %{name: "cat name here"})
If I use something like this instead in the test `%{:name => “cat name here”} I’m getting:
1) test creates category renders created category when valid (Parle.CategoryControllerTest)
test/parle_web/controllers/category_controller_test.exs:19
** (Ecto.CastError) expected params to be a map with atoms or string keys, got a map with mixed keys: %{:account_id => "91caf133-d2ae-4db0-b325-bef317f50eb3", "name" => "cat name here"}
code: conn = post(conn, Routes.category_path(conn, :create), %{:name => "cat name here"})
This one just does not make sense to me.
Changing the Map.put(payload, "account_id", account_id)
does not really help either.
I’m not coming from RoR, so to me sugar syntax is not attracting at all, and I prefer clarity, but still wants to jump on Elixir.
My main question would be, how am I suppose to pass data in the test that I can add an entry in the map so the schema validation works and still have the ~same atom/strong/keyword whatever identical everywhere.
Also if there’s an obvious way to grasp this part that I might have miss, I’d appreciate any pointer, they are just not very clear to me it appears.
Thanks