0

I'm facing the problem where all tests in which I need to assert more than one thing with a logged in user, it seems that it loses the saved session.

I have a setup code which assigns a fixture user before each test:

setup %{conn: conn} = config do
  if username = config[:login_as] do
    user = insert_user(username: username)
    conn = assign(conn, :current_user, user)
    {:ok, conn: conn, user: user}
  else
    :ok
  end
end

All my tests which happen in the logged-in area take the tag login_as: "username".

@tag login_as: "max"
test "redirects to show when data is valid", %{conn: conn} do
  conn = post conn, debit_event_path(conn, :create), debit_event: @create_attrs

  assert %{id: id} = redirected_params(conn)
  assert redirected_to(conn) == debit_event_path(conn, :show, id) #1

  conn = get conn, debit_event_path(conn, :show, id) 
  assert html_response(conn, 200) =~ "Show Debit event" #2
end

The first assertion (#1 comment in code) always works, but for some reason, the second assertion (#2 comment in code) fails and it behaves as if my user was logged out. It happens in all cases where I need to persist the user between requests.

Is there a correct approach to have this data persisted across tests requests as it was a browser request?

1
  • 4
    The lines in the tests override the initially set up conn, thus the conn = post conn... overrides the one assigned in test "...", %{conn: conn}, and later used in conn = get conn.... You could either separate the cases to separate tests, to try renaming the conn that is modified by requests eg: resp_conn = post conn... and then assert redirected_to(resp_conn) == .... The same thing for resp_conn = get conn, so the original conn is used to initiate the request/response, but the effect would be assigned to a fresh variable and not re-used for next request. Does it make sense? May 1, 2018 at 7:40

2 Answers 2

1

I was running into a similar issue, Pawel's comments helped me out... so with your example above, based on Pawel's response you could do something like the below and you should be good.

  @tag login_as: "max"
  test "redirects to show when data is valid", %{conn: conn} do
    create_conn = post conn, debit_event_path(conn, :create), debit_event: @create_attrs

    assert %{id: id} = redirected_params(create_conn)
    assert redirected_to(create_conn) == debit_event_path(create_conn, :show, id) #1

    show_conn = get conn, debit_event_path(conn, :show, id)
    assert html_response(show_conn, 200) =~ "Show Debit event" #2
  end
0

Phoenix emulates browser cookies to using Recycling

Recycling

Browsers implement a storage by using cookies. When a cookie is set in the response, the browser stores it and sends it in the next request.

To emulate this behaviour, this module provides the idea of recycling. The recycle/1 function receives a connection and returns a new connection, similar to the one returned by conn/0 with all the response cookies from the previous connection defined as request headers. This is useful when testing multiple routes that require cookies or session to work.

So if you'd like to preserve the user between requests in tests, you'd need to store a user_id in the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.