How to debug a LiveView test?

Ok, I have this test:

    @tag :integration
    test "saves new funcionario", %{conn: conn} do
      {:ok, index_live, _html} = live(conn, Routes.funcionario_index_path(conn, :index))

      assert index_live |> element("a", "New Funcionario") |> render_click() =~
               "New Funcionario"

      assert_patch(index_live, Routes.funcionario_index_path(conn, :new))

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

      %{id: empresa_id} = insert(:empresa)
      valid_fields = valid_fields()
      create_attrs = @create_attrs |> Map.merge(valid_fields) |> Map.put(:empresa_id, empresa_id)

      {:ok, _, html} =
        |> form("#funcionario-form", funcionario: create_attrs)
        |> render_submit()
        |> follow_redirect(conn, Routes.funcionario_index_path(conn, :index))

      assert html =~ valid_fields.cpf
      assert html =~ valid_fields.rg

However, it fails on the follow_redirect/3 function, giving me this error:

  1) test Index saves new funcionario (ContsWeb.FuncionarioLiveTest)
     ** (RuntimeError) LiveView did not redirect
     code: |> follow_redirect(conn, Routes.funcionario_index_path(conn, :index))
       test/conts_web/live/funcionario_live_test.exs:152: (test)

So I tried to make this path manually on a dev server. And it worked!
Could anyone explain me what exactly this error is complaining about? How it could work without errors manually and on tests it fails?

1 Like

I would add debug statements to the handle_event clause designed to handle your functionario-form submission, and make sure that the args are what you think they are, and that the handle_event clause is operating the way you expect.

Thank you! Logging out the changeset field on socket I was able to discover that a document validation was wrongly defined!