3

I have actions in my Phoenix / Plug application that have different behaviour depending on the state of the session. I wish to test this behaviour, but I am unsure how to set the session prior to making a request.

Here is an example of what I wish to be able to do:

test "GET index when not signed", %{conn: conn} do
  conn = get conn, item_path(conn, :index)
  assert html_response(conn, 302)
end

test "GET index when signed in", %{conn: conn} do
  conn = conn |> put_session(:current_user, 1)
  conn = get conn, item_path(conn, :index)
  assert html_response(conn, 200)
end

Note the attempt to set the session in the second test.

This does not work, and I get an error message like so:

** (ArgumentError) session not fetched, call fetch_session/2

But I cannot use fetch_session/1, as the session plug has not been configured here.

I could hit actions that set the session state I need, but then I have add testing hooks to my production code, which seems less than ideal.

What is the recommended way to do this?

lpil
  • 1,702
  • 1
  • 12
  • 22

0 Answers0