Testing live view form: ArgumentError for radio button when calling render_change

Hi all

I try to adapt a generated liew view to my needs and I get a failing test, I cannot make sense of.

If have this schema:

schema "accounts" do
    field :currency, Ecto.Enum, values: [:chf, :eur]
    field :name, :string
    field :retired, :boolean, default: false

    timestamps()
  end

In the edit form I use a radio button for the currency field:

<%= for currency_option <- @currency_options do %>
          <p>
            <%= radio_button f, :currency, currency_option[:value] %>
            <%= label f, :currency, currency_option[:key], 
                  for: input_id(f, :currency, currency_option[:value]) %>
          </p>
        <% end %>

In the test, the form is first submitted with invalid data (currency: nil) and the error messages are checked:

assert index_live
             |> form("#account-form", account: @invalid_attrs)
             |> render_change() =~ "can&#39;t be blank"

Unfortunately, the does throws the following error:

** (ArgumentError) value for radio "account[currency]" must be one of ["chf", "eur"], got: ""
     code: |> render_change() =~ "can&#39;t be blank"

Kind regards
Daniel

Hi all

I figured it out, by thoroughly reading the documentation :sweat_smile:

The form/3 function expects the passed data to match the actual form, therefore I need to select an option of the radio button that actually exists. This also means hidden inputs cannot be filled by form/3 but should be passed to render_change() or render_submit().

Maybe someone can make use of this information :slightly_smiling_face:

Kind regards
Daniel

1 Like