How to access cookies in helper specification?

I cannot figure out how to verify that a cookie was set when testing my helper method.

Hypothetical auxiliary 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

Does anyone know how to do this?

+5
source share
3 answers

Substituting a simple rspec mock for CookieJar works if you want:

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

I'm on rails 3.2 and rspec 2.8. Despite the fact that rspec docs says that for me this works in the request specification (i.e. Integration Test).

it 'should set cookies' do  
  cookies['foo'] = 'bar'
  visit "/"
  cookies['foo'].should == 'bar'
end
+2
source

request cookie via

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

All Articles