How to properly test LivewView when schemas have associations?

Some background

I have a web system that has a lot of associations (everything as normal here), and I’m trying to give LiveView a try.

So, my first 2 tries are failing on tests and I don’t know where to start to debug and solve.

Schemas and changesets

Business

Schema:

  schema "businesses" do
    field :address, :string
    field :branch, :string
    field :business_name, :string
    field :cnpj, :string
    field :state_registration, :string

    belongs_to :cliente, Cliente
    belongs_to :contador, Contador

    has_many :officers, Officer

    timestamps()
  end

Changeset:

  @doc false
  def changeset(business, attrs) do
    # TODO state_registration needs validation!
    business
    |> cast(attrs, @fields)
    |> validate_required(@required_fields)
    |> validate_cnpj(:cnpj)
    |> capitalize_all_words(:address)
    |> capitalize_all_words(:branch)
    |> put_assoc(:cliente, attrs.cliente)
    |> put_assoc(:contador, attrs.contador)
  end

Officer

Schema:

  schema "officers" do
    field :cnh, :string

    [...]

    belongs_to :business, Business

    timestamps()
  end

Changeset:

  def changeset(officer, attrs) do
    officer
    |> cast(attrs, @fields)
    |> validate_required(@required_fields)
    |> validate_cpf(:cpf)
    |> validate_rg(:rg_number)
    |> validate_cnh(:cnh)
    |> validate_voter_number(:voter_number)
    |> validate_phone(:phone)
    |> validate_cep(:cep)
    |> capitalize_all_words(:full_name)
    |> capitalize_all_words(:father_name)
    |> capitalize_all_words(:mother_name)
    |> capitalize_all_words(:partner_name)
    |> capitalize_all_words(:address_street)
    |> capitalize_all_words(:city)
  end

Change function:

  def change_officer(%Officer{} = officer, %Business{} = business, attrs \\ %{}) do
    officer
    |> Officer.changeset(attrs)
    |> put_assoc(:business, business)
  end

Tests

Business

Form Test

[...]
      assert index_live
             |> form("#business-form", business: @invalid_attrs)
             |> render_change() =~ "can't be blank"
[...]

Error:

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

Ok, there won’t exist any business input/nested form, I’ll need to pass on session or other way (it’ll be part of app’s business rule), so why render_change searches for business[contador]?

Edit in listing test

Test:

[...]
      assert index_live
             |> element("#business-#{business.id} a", "Edit")
             |> render_click() =~ "Edit Business"
[...]

Error:

 ** (ArgumentError) selector "#business-e7443f56-94c8-46c6-a606-e8125c409103 a" did not return any element within:

Why this part of test fails only on business_live_test and not on office_live_test if they are “identical”?

Officer

Test:

[...]
      assert index_live
             |> form("#officer-form", officer: @invalid_attrs)
             |> render_change() =~ "can't be blank"

[...]

Error:

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

Same error…

A bit late to the party, but this might solve your problem: