Liveview testing child entities

Hi All

I have a schema budget that can have many periods. I have a simple live view form that enables me to create a budget and a number of child periods. All works well. I’m now trying to create a test. So far I have the following:

@valid_attrs %{
      name: "Budget 1",
      budget_periods: [
        %{period_number: 1, budget: Decimal.new(10)},
        %{period_number: 2, budget: Decimal.new(10)},
        %{period_number: 3, budget: Decimal.new(10)},
        %{period_number: 4, budget: Decimal.new(10)},
        %{period_number: 5, budget: Decimal.new(10)},
        %{period_number: 6, budget: Decimal.new(10)},
        %{period_number: 7, budget: Decimal.new(10)},
        %{period_number: 8, budget: Decimal.new(10)},
        %{period_number: 9, budget: Decimal.new(10)},
        %{period_number: 10, budget: Decimal.new(10)},
        %{period_number: 11, budget: Decimal.new(0)},
        %{period_number: 12, budget: Decimal.new(0)}
      ]
    }

I then have the following test:

  {:ok, _, html} =
        index_live
        |> form("#budget_profile-form", budget_profile: @create_attrs)
        |> render_submit()
        |> follow_redirect(conn, Routes.budget_profile_index_path(conn, :index))

This fails with…

  ** (ArgumentError) could not find non-disabled input, select or textarea with name "budget_profile[budget_periods][]" within:

         <input name="_csrf_token" type="hidden" value="XwsWGyBZSxgvGwA3JXY7KDEOPCIQAT9i1Neaph9nCxJAvNTZb9hqRRR3"/>
         <input class="input" id="budget_profile-form_name" name="budget_profile[name]" phx-debounce="blur" type="text" value=""/>
         <input class="input" id="budget_profile-form_budget_periods_0_period_number" name="budget_profile[budget_periods][0][period_number]" phx-debounce="blur" type="number" value="1" readonly="readonly"/>
         <input class="input" id="budget_profile-form_budget_periods_0_budget" name="budget_profile[budget_periods][0][budget]" phx-debounce="blur" type="text" value="0.0"/>
         <input class="input" id="budget_profile-form_budget_periods_1_period_number" name="budget_profile[budget_periods][1][period_number]" phx-debounce="blur" type="number" value="2" readonly="readonly"/>

I guess I need to format my @valid_attrs but I’m not sure how. Has anyone come across this issue and know how to fix it?

cheers

Dave

Fixed but providing my own answer in case anyone else comes across the situation. The specific @valid_attrs format is…

@create_attrs %{
    "name" => "Sunday",
    "total" => "0.0",
    "budget_periods" => %{
      "0" => %{"budget" => "100.0", "period_number" => "1"},
      "1" => %{"budget" => "0.0", "period_number" => "2"},
      "10" => %{"budget" => "0.0", "period_number" => "11"},
      "11" => %{"budget" => "0.0", "period_number" => "12"},
      "2" => %{"budget" => "0.0", "period_number" => "3"},
      "3" => %{"budget" => "0.0", "period_number" => "4"},
      "4" => %{"budget" => "0.0", "period_number" => "5"},
      "5" => %{"budget" => "0.0", "period_number" => "6"},
      "6" => %{"budget" => "0.0", "period_number" => "7"},
      "7" => %{"budget" => "0.0", "period_number" => "8"},
      "8" => %{"budget" => "0.0", "period_number" => "9"},
      "9" => %{"budget" => "0.0", "period_number" => "10"}
    }
  }

I figured it out by simply doing an IO.inspect on the params passed to my create function. Cut and Paste, change as appropriate and all is good with the world.

cheers

Dave

1 Like