polypush135

polypush135

Following redirection in ControllerTests

Say I have a controller like so.

  def create_pass_reset(conn, %{"user" => %{"email" => email}}) do
    case Accounts.reset_pass(email) do
      {:ok, user} ->
        conn
        |> put_flash(:info, "Password Reset Sent to #{user.email}")
        |> redirect(to: session_path(conn, :new))
...
    end
  end

Note that I am setting a flash on a conn that is due to be redirected.

And say I have a test like so.

test "POST /password-reset", %{conn: conn, user: user} do
  conn = post conn, user_path(conn, :create_pass_reset, %{"user" => %{"email" => user.email}})
  assert redirected_to(conn, 302) =~ "/login"
  assert html_response(conn, 302) ==  "Password Reset Sent to #{user.email}"
end

Well clearly the issue with this test is that the conn does not contain the state after the redirection occurred and thus does not have the content we are looking for

IE:

 Assertion with == failed
 code:  assert html_response(conn, 302) == "Password Reset Sent to #{user.email()}"
 left:  "<html><body>You are being <a href=\"/login\">redirected</a>.</body></html>"
 right: "Password Reset Sent to email-11@example.com"

So my question is, how do I perform the redirect so that I can see the state after the redirection has occurred?

Marked As Solved

chrismccord

chrismccord

Creator of Phoenix

Something like this should work:

conn = post conn, user_path(conn, :create_pass_reset, %{"user" => %{"email" => user.email}})
assert "/login" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 302) ==  "Password Reset Sent to #{user.email}"
13
Post #1

Also Liked

kplattret

kplattret

Yes indeed, recycle/2 gives you a fresh connection with no assigns, so you need to save them prior to calling it and make sure you put them again. I encountered the same scenario and found a solution here: Why Phoenix Recycling?

chrismccord

chrismccord

Creator of Phoenix

It’s just fine pattern wise. It’s not exactly the same, but our context generators make use of redirected_params to construct a path and then get the show page of a created resource:

https://github.com/phoenixframework/phoenix/blob/444569fb99a599d67ec16fd492c3681412dd5759/priv/templates/phx.gen.html/controller_test.exs#L33-L35

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement