[rspec-users] Cookies - how to set in controller specs?

Christopher Bailey chris at cobaltedge.com
Wed Jul 2 13:42:24 EDT 2008


First, nevermind!  Oy!  I finally figured it out.  The reason it
wasn't working in my controller code was that I was checking for
"cookies[:cookie_key]", not "request.cookies[:cookie_key]"!  It's a
bit strange how that manifested, given the fact that referencing just
"cookies" was a hash with values, but alas, that's what was happening.

So, thank you very much for your time (that I essentially wasted :(

On Wed, Jul 2, 2008 at 9:57 AM, Christopher Bailey <chris at cobaltedge.com> wrote:
> On Wed, Jul 2, 2008 at 9:41 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
>> On Jul 2, 2008, at 11:15 AM, Christopher Bailey wrote:
>>
>>> On Wed, Jul 2, 2008 at 4:06 AM, David Chelimsky <dchelimsky at gmail.com>
>>> wrote:
>>> <snip>
>>>>
>>>> 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
>>>
>>>  cookies[:cookie_key][:cookie_key]
>>
>> Sorry Christopher - try this:
>>
>> request.cookies[:cookie_key] = 'cookie value'
>
> I tried that (see below in my email - I just mistakenly wrote it
> without the "request." at the beginning).  When I do this, it appears
> to set it, but then trying to retrieve it in my controller fails (even
> though the key is there, and the value is there, when then requesting
> cookies[:cookie_key] I get no value back).  Pretty weird.
>
>
>> Cheers,
>> David
>>
>>> 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"]
>>>>
>>>> or
>>>>
>>>> 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.
>>>>
>>>> HTH,
>>>> David
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-users at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>>
>>>
>>>
>>> --
>>> Christopher Bailey
>>> Cobalt Edge LLC
>>> http://cobaltedge.com
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-users at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>
>
>
> --
> Christopher Bailey
> Cobalt Edge LLC
> http://cobaltedge.com
>



-- 
Christopher Bailey
Cobalt Edge LLC
http://cobaltedge.com


More information about the rspec-users mailing list