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.

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.

2 Likes

Just got the same problem writing tests for phoenix, it’s really unexpected.