#Need more information debugging an assert_patch test

1 messages · Page 1 of 1 (latest)

naive raven
#

I think the form might be throwing an error. Probably the easiest way is to add some debug prints to your handle_event("save", ...) so you can see if it's returning a new form with errors, most likely due to the changeset being invalid.

wild pond
#

Greetings!
One of the only post about assert_patch, I need. Though, the original message was deleted.

I generated live components of an existing context i.e. Users. However, the generated tests are failing on every assert_patch(live_view, ~p/users_route) .

  • So now, I am getting failed test only for events :new, :edit on index, show, but they work fine on dev without any warning or errors.
  • Expected TDD green phase.

Example for :edit test

    test "updates user in listing", %{conn: conn, user: user} do
      {:ok, index_live, _html} = live(conn, ~p"/users")
      assert index_live |> element("#users-#{user.id} a", "Edit") |>                         render_click() =~
               "Edit User"
      assert_patch(index_live, ~p"/users/#{user}/edit") # This one passed
      assert index_live
             |> form("#user-form", user: @invalid_attrs)
             |> render_change() =~ "can't be blank"
      assert index_live
             |> form("#user-form", user: @update_attrs)
             |> render_submit()
      assert_patch(index_live, ~p"/users")   # HERE
       ..
    end

Log trace:

* test Index updates user in listing
     ** (ArgumentError) expected MyAppWeb.UserLive.Index to patch to "/users", but got none       
     code: assert_patch(index_live, ~p"/users")
     stacktrace:
       (phoenix_live_view 1.0.18) lib/phoenix_live_view/test/live_view_test.ex:1453: Phoenix.LiveViewTest.assert_navigation/4
       (phoenix_live_view 1.0.18) lib/phoenix_live_view/test/live_view_test.ex:1344: Phoenix.LiveViewTest.assert_patch/3
       test/myapp_web/live/user_live_test.exs:65: (test)

Modal where patch is set (index.html):

<.modal :if={@live_action in [:edit]} id="user-modal" show on_cancel={JS.patch(~p"/users")}>
  <.live_component
    module={MyAppWeb.UserLive.FormComponent}
    ...
    patch={~p"/users"}    # SET
  />
</.modal>

Please, what am I missing? How to debug? Thanks for the time.

naive raven
#

If you look at the save_user/3 function in your "form_component.ex" you'll see that it only does a push_patch(to: socket.assigns.patch) if the call to Accounts.update_user/2 returns {:ok, user}. So why isn't it? To debug things like this, I'll put in a dbg() to see what the changeset looks like in the error case, which will include errors. So change save_user to print the changeset when it errors, and rerun the test.

      {:error, %Ecto.Changeset{} = changeset} ->
        dbg(changeset)
        {:noreply, assign(socket, form: to_form(changeset))}

Run just that test:

$ mix test test/myapp_web/live/user_live_test.exs:49

You should see something like:

[lib/myapp_web/live/user_live/form_component.ex:64: MyappWeb.UserLive.FormComponent.save_user/3]
changeset #=> #Ecto.Changeset<
  action: :update,
  changes: %{name: "some updated name"},
  errors: [age: {"can't be blank", [validation: :required]}],
  data: #Myapp.Accounts.User<>,
  valid?: false,
  ...
>
wild pond
#

Thank you very, it was exactly a changeset I forgot about in addition to dbg in the right place as you suggested!
And apologies for double posting!