New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functional test get method ignores session variables from setup when given a hash of new session variables #1529
Comments
This appears to be the intention as the code explicitly creates a new session. You can easily merge it yourself - I'd recommend using update as the session hash is a Rack session hash and update is redefined to stringify keys, e.g:
|
Thanks for the quick response and the session.update tip. I know merging it myself isn't hard but isn't merging the common case? Any thoughts on why the session is treated differently from the parameters and the flash, both of which are merged in (around the code you linked to)? |
Well if it merged by default how would you create a test with a clean session - you'd need something like this: class SomeControllerTest < ActionController::TestCase
setup do
@request.session["user_id"] = 1
end
test "some action" do
@request.session = ActionController::TestSession.new
get :some_action, { }, { :another_var => 2 }
end
end It would also be a breaking change without any possibility of being backwards compatible unless you add a config option to enable the old behaviour. |
If merging the session is more common, I wouldn't mind the additional @request.session = ActionController::TestSession.new in the odd case I'd need to create a clean session but I do see your point about backwards compatibility. |
I do think merging the session is the more common behavior. @pixeltrix : with merging session params, there isn't necessarily a need to reinitialize the session manually.
This way, you have all your tests use the same predefined session depending of the context. |
If you're going to try and stick to Semantic Versioning then it shouldn't come before 4.0 :-) |
I'm not a stickler for version numbers. Don't mind waiting til 3.2 or later. Should I contribute a patch with tests? |
Any more thoughts on this? Go forward or ignore? |
Ignore it is. |
@avk current focus is getting 3.1 out the door - this won't be patched before 3.2 so not a high priority right now. I'll assign it to me so it doesn't get lost and give you an answer once 3.1 final has shipped. |
Awesome, thanks. Just wanted to make sure it didn't get lost. |
@pixeltrix ping. |
@pixeltrix was this eventually added to 3.2? @avk, are you still seeing this problem? |
@steveklabnik looking at it now - if it's not in then I'll add it today |
Awesome, thanks. ❤️ |
Just out of interest would it be preferable to reset to the initial session after a class SomeControllerTest < ActionController::TestCase
setup do
session[:user_id] = 1
end
test "some action" do
get :some_action, { }, { :another_var => 2 }
# session == { "user_id" => 1, "another_var" => 2 }
get :some_action
# A: session == { "user_id" => 1 }
# B: session == { "user_id" => 1, "another_var" => 2 }
end
end Which would be preferable - A or B. I can see arguments for both. |
Form me B if it is in the same test as you showed. But A if it is in different tests. |
The request instance is re-created in the test setup phase so that would always be the case. The B case is nice and easy so lets go with that :-) |
Sorry to have missed out on the conversation earlier today but glad to see my suggestion made it in. The B case makes sense to me too. |
I believe this is a functional test bug:
After the
get
line, the session will only haveanother_var
, notuser_id
. To get the latter, you have to do:@request.session.merge({ "another_var" => 5 })
as the third argument toget
. It does not seem to matter whether the session keys are strings or symbols, both fail without a merge.Shouldn't merging with the current
@request.session
be done as part ofget
? Happy to start a patch if we agree this is a bug.The text was updated successfully, but these errors were encountered: