RSpec / RSpec Rails / Docs / Controller specs
Recommended guidelines for rails-3.0.0 to 3.1.0
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:
Recommended guidelines for rails-3.0.0 to 3.1.0
- Access cookies through the
requestandresponseobjects in the spec. * Userequest.cookiesbefore the action to set up state. * Useresponse.cookiesafter the action to specify outcomes. - Use the
cookiesobject in the controller action. - Use String keys.
<pre>
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 </pre>
Why use Strings instead of Symbols?
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.
Why not use the cookies method?
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_actionFuture versions of Rails
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.
Source: features/controller_specs/Cookies.md at v2.12.2, RSpec Rails (MIT license).