Skip to content
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

Cannot write/read cookies in spec #287

Closed
dnagir opened this issue Dec 15, 2010 · 7 comments
Closed

Cannot write/read cookies in spec #287

dnagir opened this issue Dec 15, 2010 · 7 comments

Comments

@dnagir
Copy link

dnagir commented Dec 15, 2010

The controller spec below fails for me.

describe SuggestionsController do
  it 'should read/write cookies' do
    cookies[:name] = 'value'
    cookies[:name].should == 'value'
  end

  it 'should preserve cookie' do
    cookies[:name] = 'value'
    get :index
    cookies[:name].should == 'value'
  end

end

Not sure why, as looking at controller_example_group_spec it should work.

@sapientpants
Copy link

I'm having the same issue in my specs as well, so it's not just you.

@dchelimsky
Copy link
Contributor

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.

@dnagir
Copy link
Author

dnagir commented Dec 24, 2010

I think I was also not able to set cookie in spec and read it after the action from spec.

@dchelimsky
Copy link
Contributor

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?

@dnagir
Copy link
Author

dnagir commented Dec 24, 2010

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 cookies so we explicitly use request and response objects?).

@dchelimsky
Copy link
Contributor

It's not RSpec's to deprecate :) All three of these ways to access cookies come directly from Rails.

@dchelimsky
Copy link
Contributor

JonRowe added a commit that referenced this issue Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants