Testing LiveView redirects - "(EXIT) ... process is not alive"

I have an app which uses phx.gen.auth for user auth, so the login is handled by a standard controller, but everything else in the app uses LiveView.

Now, I want to write a test for the functionality where the user is not logged-in yet, attempts to take an action, and is redirected to the login page. The implementation is like this:

cond do
  is_nil(socket.assigns.active_user) ->
    push_redirect(socket, to: "/users/log_in")

And the test like this:

{:ok, view, _html} = live(conn, "/")

view
|> element("#new-post-button")
|> render_click

But now matter how I try to assert afterwards, I get the following error:

** (EXIT) no process: the process is not alive or there's no process currently 
associated with the given name, possibly because its application isn't started

I know this is because the login page is a dead view, so the LV process quits. But there must be some way to test this and confirm that we did in fact go to the login page, and that we’re not still in the LV seeing the “create post” form. I’m also curious about other situations where I am leaving the current LV and moving to a different one - what is the proper way to test these scenarios?

I’ve tried assert_redirected/2 and follow_redirect/3 from Phoenix.LiveViewTest with no success. Phoenix.ConnTest.get(conn, "/users/log_in") also doesn’t seem right here because I don’t want to test the page itself, but rather the redirect to that page.

Thanks in advance for any help on this issue.

I’ve been struggling with a similar thing. I can successfully test for the redirect but can’t take actions in the new liveview (which I redirect to using backend push_redirect)

{:ok, orig_view, _html} = live(conn, orig_path)
orig_view |> element("#mybutton") |> render_click()
assert_redirect(orig_view, new_path)
# Ok so far. I have a successful redirct
# But how to a click a button on the new page?
# Is there anywhere I can grab the new LV pid from?

Oh, I almost forgot about this thread! I ended up using

{:ok, new_conn} = live(conn, "my_path") |> follow_redirect(conn)

for this kind of test. But my redirect leads to a deadview. Here is some instruction from the docs about using it for LV also:

If the LiveView redirects with a live redirect, this macro returns {:ok, live_view, disconnected_html} with the content of the new LiveView, the same as the live/3 macro. If the LiveView redirects with a regular redirect, this macro returns {:ok, conn} with the rendered redirected page. In any other case, this macro raises.

Source: Phoenix.LiveViewTest — Phoenix LiveView v0.20.2

1 Like