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

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New

We're in Beta

About us Mission Statement