`LiveViewTest.follow_redirect()` doesn't work for `push_patch`

The form save event:

def handle_event("save", %{"todo" => todo_params}, socket) do
     |> save_todo(todo_params)
     |> push_patch(to: socket.assigns.return_to)

Inside the test:

{:ok, _view, refreshed_html} = view
        |> form("#todo-form", %{todo: ...})
        |> render_submit()
        |> follow_redirect(conn)

assert refreshed_html =~ "Todo created successfully"

Error message:

** (RuntimeError) LiveView did not redirect
     code: |> follow_redirect(conn)

I found the solution :partying_face:

# Submit the form. Notice here the actions performed mutates the view such that the changes we perform on it can be checked later.
  |> form("#todo-form",%{todo: ...})
  |> render_submit()

# Check if the entire rendered view shows the flash message.
assert view |> render() =~ "Todo created successfully"

Just for clarity’s sake, push_patch is by definition not a redirect - it patches the DOM of the current LiveView. That means you assert on the rendered html of the same LV process which you have figured out

Push_redirect on the other hand would require you to call follow_redirect

1 Like