oboudry

oboudry

Troubleshooting a failed test 302 redirect instead of 200

Hi,

I’m reading the Pragmatic Programmer’s book “Programming Phoenix 1.0”, but adapting the code examples to Phoenix 1.3. Also it’s a bit of a struggle I find it very instructive. It’s a good way to experiment the changes introduced by Phoenix 1.3.

I did not delete all the generated tests as suggested in the book, but kept them and worked at making them pass. I managed to make most tests pass, except the following one that I’m really struggling with. Instead of displaying the created video and returning status 200, I seem to loose the current_user assign in my connection, and therefore no longer be authorized and be redirected to root with a status 302.

I did Pry into my tests to try to understand what happens. And it looks like conn is modified, with current_user entry being lost, around the IEx.Pry in the code below.

  describe "create video" do
    setup [:login_user]

    test "redirects to show when data is valid", %{conn: conn, user: user} do
      attrs = Map.put(@create_attrs, :user_id, user.id)
      conn = post conn, video_path(conn, :create), video: attrs

      assert %{id: id} = redirected_params(conn)
      assert redirected_to(conn) == video_path(conn, :show, id)

      require IEx; IEx.pry
      conn = get conn, video_path(conn, :show, id)
      assert html_response(conn, 200) =~ "Show Video"
    end

    test "renders errors when data is invalid", %{conn: conn} do
      conn = post conn, video_path(conn, :create), video: @invalid_attrs
      assert html_response(conn, 200) =~ "New Video"
    end
  end

When I run the line right after the Pry, I get an error, but I’m not sure if that error happens during tests, and causes the loss of the assigns in my connection, or if it just fails when debugging because in Pry the context might be different.

Request to pry #PID<0.314.0> at RumblWeb.VideoControllerTest."test create video redirects to show when data is         valid"/1 (test/rumbl_web/controllers/video_controller_test.exs:56)

   54:       assert redirected_to(conn) == video_path(conn, :show, id)
   55:
   56:       require IEx; IEx.pry
   57:       conn = get conn, video_path(conn, :show, id)
   58:       assert html_response(conn, 200) =~ "Show Video"

Allow? [Yn]
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
pry(1)> conn = get conn, video_path(conn, :show, id)
** (ArgumentError) could not call get_attribute with argument RumblWeb.VideoControllerTest because the module is already compiled
    (elixir) lib/module.ex:1468: Module.assert_not_compiled!/2
    (elixir) lib/module.ex:1327: Module.get_attribute/3
    (elixir) lib/kernel.ex:2500: Kernel.do_at/5
    (elixir) expanding macro: Kernel.@/1
    test/rumbl_web/controllers/video_controller_test.exs:1: RumblWeb.VideoControllerTest."test create video redirects to show when data is valid"/1
    (phoenix) expanding macro: Phoenix.ConnTest.get/2
    test/rumbl_web/controllers/video_controller_test.exs:1: RumblWeb.VideoControllerTest."test create video redirects to show when data is valid"/1
pry(1)> 14:56:45.633 [error] Postgrex.Protocol (#PID<0.282.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.314.0> timed out because it owned the connection for longer than 15000ms

Inspecting conn at the IEx.Pry I can still find the :current_user assign in my conn map, but if I move the IEx.Pry one line lower, current_user is no longer in the conn.assigns.

I must say I also don’t understand much to the error I get when running line 57, and am not sure if it happens systematically or only in when in IEx.pry.

conn = get conn, video_path(conn, :show, id)
** (ArgumentError) could not call get_attribute with argument RumblWeb.VideoControllerTest because the module is already compiled

I’m not calling get_attribute but the test environment might be.

Any help troubleshooting this would be really appreciated.

Thanks,

Olivier.

Marked As Solved

oboudry

oboudry

The problem I was facing was recycling of the connection. Did a manual recycle between calls and it’s now working as it should. Solution is described on stackoverflow: https://stackoverflow.com/questions/46363292/losing-conn-assigns-in-the-middle-of-a-test/46363884#46363884 sharing in case other face this problem.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

Other popular topics Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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

We're in Beta

About us Mission Statement