Phoenix: Error on generated controller test with map attributes

Created on 4 Jan 2020  路  2Comments  路  Source: phoenixframework/phoenix

  • Elixir version (elixir -v): Elixir 1.9.4 (compiled with Erlang/OTP 22)
  • Phoenix version (mix deps): 1.4.11
  • Jason : 1.1.2
  • Operating system: MacOs
  • config :phoenix, :json_library, Jason

Hello, I'm sure it is a known issue but I could not find a duplicate.

With this command: mix phx.gen.json Content Version versions changelog_id:references:changelogs datamap:map metamap:map I generated a test: test/myapp_web/controllers/version_controller_test.exs.

The test for "create version" is correct:

  describe "create version" do
    test "renders version when data is valid", %{conn: conn} do
      conn = post(conn, Routes.version_path(conn, :create), version: @create_attrs)
      assert %{"id" => id} = json_response(conn, 201)["data"]

      conn = get(conn, Routes.version_path(conn, :show, id))

      assert %{
               "id" => id,
               "datamap" => %{},
               "metamap" => %{}
             } = json_response(conn, 200)["data"]
    end

    test "renders errors when data is invalid", %{conn: conn} do
      conn = post(conn, Routes.version_path(conn, :create), version: @invalid_attrs)
      assert json_response(conn, 422)["errors"] != %{}
    end
  end

But the test for "update version" is incorrect as it tries to match empty tuples and not maps in the last assert:

  describe "update version" do
    setup [:create_version]

    test "renders version when data is valid", %{conn: conn, version: %Version{id: id} = version} do
      conn = put(conn, Routes.version_path(conn, :update, version), version: @update_attrs)
      assert %{"id" => ^id} = json_response(conn, 200)["data"]

      conn = get(conn, Routes.version_path(conn, :show, id))

      assert %{
               "id" => id,
               "datamap" => {},
               "metamap" => {}
             } = json_response(conn, 200)["data"]
    end
bug starter

Most helpful comment

Fixed as mentioned, thank you @lud for reporting and thank you @sushant12 for verifying!

All 2 comments

@lud @chrismccord I believe this is fixed in master branch.

Previosuly, it was

assert %{
               "id" => id<%= for {key, val} <- schema.params.update do %>,
               "<%= key %>" => <%= Phoenix.json_library().encode!(val) %><% end %>
             } = json_response(conn, 200)["data"]

it enocde the val but didnt decode and inspect the val

which is fixed in master

assert %{
               "id" => id<%= for {key, val} <- schema.params.update |> Phoenix.json_library().encode!() |> Phoenix.json_library().decode!() do %>,
               "<%= key %>" => <%= inspect(val) %><% end %>
             } = json_response(conn, 200)["data"]

Fixed as mentioned, thank you @lud for reporting and thank you @sushant12 for verifying!

Was this page helpful?
0 / 5 - 0 ratings