Issue with Phoenix 1.5 Live Form

So, I have a phx 1.5 live edit for that was generated via mix phx.gen.live, and I added a belongs_to association to. I edited the form to have a reference to that relation, but I keep getting errors when mix test tries to fill out the form:

    <% inputs_for f, :room_type, fn it -> %>
      <%= label it, :id, "Room Type", class: "block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2"%>
      <%= select it, :id, Enum.map(@room_types, &{&1.description, &1.id}), class: "appearance-none block w-full bg-gray-200 text-gray-700 border rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white"%>
      <%= error_tag it, :id, class: "text-red-500 text-xs italic pt-3" %>
    <% end %>

Section of test code:

    @room_type %{id: 1, name: "some name", description: "some room type"}
    @valid_attrs %{description: "some description", room_type: @room_type, name: "some name", owner: "some owner"}
...
      {:ok, _, html} =
        index_live
        |> form("#room-form", island: @create_attrs)
        |> render_submit()
        |> follow_redirect(conn, Routes.room_index_path(conn, :index))

But I’m getting:

** (ArgumentError) could not find non-disabled input, select or textarea with name "room[room_type]" within:
         <input name="_csrf_token" type="hidden" value="AjI4EDkKMwB5DzhZBAUhZn9cbDlyMixXakkAmRBKTHO5hwE660ZjJwy3"/>
         <input class="" id="room_name" name="room[name]" type="text"/>
         <input class="" id="room_owner" name="room[owner]" type="text"/>
         <textarea class="" id="room_description" name="room[description]">
        </textarea>
         <select class="" id="room_room_type_id" name="room[room_type][id]"><option value="1">some room type</option><option value="2">some other room type</option></select>

Does anyone have any ideas on how I can test this properly?

Okay, I figured this out, it turns out it wasn’t failing in the create scenario, it was failing in the invalid_attrs scenario which happens to be in the same named “save” test that the generator creates:

@invalid_attrs %{description: nil, room_type: nil, name: nil, owner: nil}
...
test "saves new room", %{conn: conn} do
...
  assert index_live
             |> form("#room-form", room: @invalid_attrs)
             |> render_change() =~ "can&apos;t be blank"

Because I am using existing values for the select, I had to use an existing room_type

- @invalid_attrs %{description: nil, room_type: nil, name: nil, owner: nil}
+ @invalid_attrs %{description: nil, room_type: %{id: 1}, name: nil, owner: nil}

And everything is now hunky-dory.

1 Like