-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Cannot write/read cookies in spec #287
Comments
I'm having the same issue in my specs as well, so it's not just you. |
To narrow this down a bit, you can write cookies in the controller and read them in the spec after the action: # spec it "sets a cookie" do get :index cookies[:this].should eq("that") end # controller def index cookies[:this] = "that" end What is not working is the ability to set a cookie in the spec and read it in the controller. |
I think I was also not able to set cookie in spec and read it after the action from spec. |
OK - here's what's up. There are actually two sets of cookies exposed in a Rails functional test (which is the basis for an RSpec controller spec): @request.cookies and @response.cookies. The object returned by the cookies method is the two set merged (https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_process.rb#L25), but if want to set cookies on the request, you have to do it explicitly. @request.cookies["foo"] = "bar" @request.cookies["foo"].should eq("bar") cookies["foo"].should eq("bar") Now rspec-rails-1 did have some hax so that the cookies method returned @request.cookies before the action, but I'm not sure I want to add that to rspec-rails-2 on the grounds that it's not 100% reliable and might lead to confusing results. I think being explicit (request.cookies/response.cookies) is the best way to go. WDYT? |
Personally for me that's absolutely ok. Hopefully other people will not be confused by it. But maybe it would make sense to eliminate the confusion somehow (deprecate |
It's not RSpec's to deprecate :) All three of these ways to access cookies come directly from Rails. |
The controller spec below fails for me.
end
Not sure why, as looking at controller_example_group_spec it should work.
The text was updated successfully, but these errors were encountered: