Controller specs wrap Rails controller tests, which expose a few different ways to access cookies:
@request.cookies['key']
@response.cookies['key']
cookies['key']
rails-3.0.x and 3.1 handle these slightly differently, so to avoid confusion, we recommend the following guidelines:
- Access cookies through the
request
andresponse
objects in the spec.- Use
request.cookies
before the action to set up state. - Use
response.cookies
after the action to specify outcomes.
- Use
- Use the
cookies
object in the controller action. - Use String keys.
# 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
The cookies
objects in the spec come from Rack, and do not support
indifferent access (i.e. :foo
and "foo"
are different keys). The cookies
object in the controller does support indifferent access, which is a bit
confusing.
This changed in rails-3.1, so you can use symbol keys, but we recommend sticking with string keys for consistency.
The cookies
method combines the request
and response
cookies. This can
lead to confusion when setting cookies in the example in order to set up state
for the controller action.
# does not work in rails 3.0.0 > 3.1.0
cookies['foo'] = 'bar' # this is not visible in the controller
get :some_action
There is code in the master branch in rails that makes cookie access more
consistent so you can use the same cookies
object before and after the action,
and you can use String or Symbol keys. We'll update these docs accordingly when
that is released.