Test controller passing lists inside maps

Hi all, I’m trying to test my controller but I get an error regarding maps inside lists. This is an example:

test "create classifier", %{conn: conn} do
      classifier = %{
        classifier: %{
          threshold: 15,
          perks: [
            %{points: 10, description: "desc_1"},
            %{points: 12, description: "desc_2"}
          ]
        }
      }

      conn = post(conn, Routes.classifier_path(conn, :create, classifier))

      assert json_response(conn, 200)["data"] == %{
               "threshold" => 15
             }
    end

However, I get this error (ArgumentError) cannot encode maps inside lists when the map has 0 or more than 1 element, got: %{points: 10, description: "test_desc"}

How can I test this endpoint?

I think you want to send classifier as POST params, so it should be passed as 3rd argument to post/3 function.

1 Like

@fuelen my bad, you are right! thank you

the solution is:

test "create classifier", %{conn: conn} do
      classifier = %{
        classifier: %{
          threshold: 15,
          perks: [
            %{points: 10, description: "desc_1"},
            %{points: 12, description: "desc_2"}
          ]
        }
      }

      conn = post(conn, Routes.classifier_path(conn, :create), classifier)

      assert json_response(conn, 200)["data"] == %{
               "threshold" => 15
             }
    end
1 Like