28

Helper Method

  # Determine if this is user's first time
  def first_time?
    cookies[:first_time].nil?
  end

Attempted Rspec test

it "returns true if the cookie is set" do
  cookies[:first_time] = "something"
  helper.first_time?().should be(true)
end

Error:

undefined method `cookies' for nil:NilClass

Everything I've read about Rspec and cookies has to do with the controller. Any way to get/set cookies in Rspec helper tests?

(Rspec/Rspec-rails 2.5, Rails 3.0.4)

Thanks!!

UPDATE:

Found an answer on how to SET cookies, so I'll leave it here for other's reference.

the piece I was looking for:

helper.request.cookies[:awesome] = "something"

Still don't know how to GET cookies...

jmccartie
  • 4,956
  • 8
  • 50
  • 71
  • Just curious, why would you want to GET cookies? In specs you are always the one that sets the cookies, so you know what they are. Don't you just want to test that your app behaves a certain way with the cookies set a certain way? – radixhound Aug 04 '12 at 15:35
  • GET is just `helper.request.cookies[:awesome]` – Michael Durrant Apr 20 '16 at 22:13

5 Answers5

22

The best explanation I've been able to find is here: https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies

Quoted:

Recommended guidelines for rails-3.0.0 to 3.1.0

  • Access cookies through the request and response objects in the spec.
  • Use request.cookies before the action to set up state.
  • Use response.cookies after the action to specify outcomes.
  • Use the cookies object in the controller action.
  • Use String keys.

Example:

# spec
request.cookies['foo'] = 'bar'
get :some_action
response.cookies['foo'].should eq('modified bar')

# controller
def some_action
  cookies['foo'] = "modified #{cookies['foo']}"
end
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
  • Also note that if you set cookies with symbols like `cookies[:foo] = true` you will have key and value converted to string `cookies['foo'] = 'true'` – Calin Oct 28 '13 at 14:07
6

I just stumbled upon your question (I was looking for an answer). I tried this successfully:

helper.request.cookies[:foo] = "bar"
apneadiving
  • 114,565
  • 26
  • 219
  • 213
4

None of the answers worked for me on Rails 6.1.3, but after much testing, the following worked:

helper.cookies[:foo] = "bar"

And for signed cookies:

helper.cookies.signed[:foo] = "bar"
Alexander
  • 3,959
  • 2
  • 31
  • 58
  • 1
    I found this to be true in Rails 5.0 as well. I suspect it's more related to what version of rspec-rails you are using. This should be the accepted answer for modern-day Rails/RSpec installs. – Jeff Seifert Jul 19 '21 at 17:42
0

In a controller test this works:

@request.cookies[:cookie_name] = "cookie_value"

in a before block.

I found this here

MERM
  • 629
  • 7
  • 21
0

You get the cookies the same way you set them. Example from one of my specs:

request.cookies[:email].should be nil
jhwist
  • 15,201
  • 3
  • 40
  • 47
  • 1
    Strange. If I place that in my helper spec, I get "undefined local variable or method `request'". if i change it to helper.request I get nil (though it works fine functionally and the code is solid). Argh. – jmccartie Apr 01 '11 at 04:14
  • here's the spec I'm trying to run and its results: https://gist.github.com/897722 – jmccartie Apr 01 '11 at 04:19