5

I can't figure out how to test that a cookie has been set when testing my helper method.

Hypothetical helper method:

def my_helper(k,v)
    cookies[k] = v
end

Test:

it 'should set cookies' do  
  helper.my_helper("foo", "bar")
  helper.cookies["foo"].should == "bar" #nil
  helper.response.cookies["foo"].should == "bar" #nil
end

Anyone know how to do this?

3

3 Answers 3

4

Substituting a simple rspec mock for the CookieJar works, if you're willing to:

helper.stubs(:cookies => cookies = mock)
cookies.expects(:[]=).with('foo', 'bar')
helper.my_helper('foo', 'bar')
2

I'm on rails 3.2 and rspec 2.8. Despite what the rspec docs says the following works for me in a request spec (ie. integration test).

it 'should set cookies' do  
  cookies['foo'] = 'bar'
  visit "/"
  cookies['foo'].should == 'bar'
end
4
  • Yup, I've definitely checked cookies in standard request specs.
    – Matt Baker
    Jul 26, 2012 at 0:52
  • @MattBaker Which version of rails and rspec you're using?
    – Dty
    Jul 26, 2012 at 0:59
  • @MattBaker also here an article about how someone debugged their way through using cookies in rspec. It might help you figure out how to troubleshoot this whole thing. dobbse.net/thinair/2011/12/…
    – Dty
    Jul 26, 2012 at 1:01
  • We're on rails 3.1.4 and rspec 2.8. Maybe I'll take a shot at upgrading.
    – Matt Baker
    Jul 26, 2012 at 1:13
0

request the cookie through

 helper.request.cookies[:awesome] = "something"
1
  • This doesn't work for me. Remember, I'm trying to get the cookie, not set it.
    – Matt Baker
    Jul 25, 2012 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.