[rspec-users] Cookies - how to set in controller specs?
chris at cobaltedge.com
Wed Jul 2 12:15:27 EDT 2008
On Wed, Jul 2, 2008 at 4:06 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
> This is just one of those goofy things in Rails testing. I'm not sure
> the best way to make it easier in rspec without breaking existing
> examples in the process. Regardless, here's how you interact with
> cookies from an example:
> To set a cookie:
> request.cookies[:cookie_key] = CGI::Cookie.new('cookie_key', 'cookie value')
When I do this, in order to get to this cookie in my controller code,
I have to do
Basically, it appears that what it does is assign that key a hash of
its own. That makes sense of course, as I realize a cookie is really
a hash of name, value, path, expires, and so on. However, it doesn't
jive with the retrieval, as you shouldn't have to double reference it
(which I believe is essentially the point of the  method on
ActionController::CookieJar and is not how things are documented).
However, what's really behaving weird, is if I do:
cookies[:cookie_key] = "1234"
Then, in my controller code, if I look at "cookies", it shows that
cookies is a hash, and if I call .keys on it, it spits out
":cookie_key", and if I call .values on it, it says "1234", but if I
then go and do cookies[:cookie_key], it gives me nil.
Again, I have to suspect something weird going on with Rails test
environment/RSpec, since all this works fine outside of tests. Any
suggestions on how to debug this further or what might be wrong?
I should note I'm using Rails 2.1, and RSpec and rspec-rails from
about a week ago (from GitHub).
> To read a cookie
> response.cookies[:cookie_key].should == ["expected value"]
> cookies[:cookie_key].should == ["expected value"]
> Rails provides a cookies object that is actually response.cookies, so
> you don't *have* to reference it through the response object. I would,
> however, as I've been known to try to set a cookie in an example using
> cookies when I should have been using request.cookies. So I try to
> keep them explicit.
> rspec-users mailing list
> rspec-users at rubyforge.org
Cobalt Edge LLC
More information about the rspec-users