When I call Plug.Conn.fetch_session with a conn created by the conn_test file, I get the error:
** (ArgumentError) cannot fetch session without a configured session plug
(plug) lib/plug/conn.ex:701: Plug.Conn.fetch_session/2
Not 100% sure, but it seems like a bug since fetch_cookies works perfectly fine. It seems like phoenix should configure the session plug for me?
This is not a bug. The preferred approach is to test through your endpoint, which fetches the session for you and applies all your plug transformations. My guess is you are fetching the session because you want to put_session to "stub" a session value? You have two options in this case, one is to make a request to a path that authorizes that user, then "recycle" that conn to your next request, or 2, you can put a private assign into the conn in the test and then your application code checks for the private assign and fallback to session. We could do better with examples in our testing guides, but those are your best options.
thanks @chrismccord! I'll go with the recycle method.
As a side note, I'm wondering if this restriction would make feature tests prohibitively slow. Thoughtbot's https://github.com/thoughtbot/clearance runs blazing fast because it has a backdoor for user signin. It seems terribly inefficient to so a user login test before every other test that requires login.
Personally, one of the biggest draws to elixir is fast / asyc tests, so hopefully it doesn't make a perceptible difference.
As a side note, I'm wondering if this restriction would make feature tests prohibitively slow.
In other languages yes. In Phoenix, no!!! Still blazing fast, no hacks/dependenices required :)
If you want to backdoor the user, use the private assigns like I said, but honestly tests are going to be fast when going through the endpoint.
@chrismccord Cool! Can you point me to an article or spot where private assigns are used?
Hello @chrismccord ,
is it still required to recycle conn between requests as of Phoenix v. 1.2?
Looks like it happens automatically within Phoenix.ConnTest.dispatch/5 https://github.com/phoenixframework/phoenix/blob/v1.2.0/lib/phoenix/test/conn_test.ex#L223
But
conn =
build_conn
|> get("/")
|> put_session(:user_id, user.id)
|> post(...)
is throwing ** (Plug.Conn.AlreadySentError) the response was already sent
Yes, you need to recycle. If you call get right after post it will try to send the response twice.
Most helpful comment
This is not a bug. The preferred approach is to test through your endpoint, which fetches the session for you and applies all your plug transformations. My guess is you are fetching the session because you want to
put_sessionto "stub" a session value? You have two options in this case, one is to make a request to a path that authorizes that user, then "recycle" thatconnto your next request, or 2, you can put a private assign into the conn in the test and then your application code checks for the private assign and fallback to session. We could do better with examples in our testing guides, but those are your best options.