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 #2

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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
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 126479 1222
New

We're in Beta

About us Mission Statement