Topic: Cookies in Functional Tests

Hi all,

having trouble accessing the returned cookies in my functional tests. First thing is that I cannot access it by cookies[:name] but rather have to do it by cookies["name"]. Not sure why this is but its no biggie.

The bigger head scrather though is why cookies["name"].value always returns an array even though the data going into that array from the controller is a simple string "testvalue"

Any thoughts or answers greatly appreciated

Thanks
Garrett

Re: Cookies in Functional Tests

garrett.heaver wrote:

having trouble accessing the returned cookies in my functional tests. First thing is that I cannot access it by cookies[:name] but rather have to do it by cookies["name"]. Not sure why this is but its no biggie.

For some reason the cookies method returns Hash instead of a HashWithIndifferentAccess which allows you to use symbols and strings interchangeably for hash keys. I wish they would be more consistent with this...

garrett.heaver wrote:

The bigger head scrather though is why cookies["name"].value always returns an array even though the data going into that array from the controller is a simple string "testvalue"

CGI::Cookie (which exists outside of Rails) stores the values in an array. I'm not really sure why or even how this is handled on the browser side. I'd just call cookies['name'].value.first.

Last edited by ryanb (2006-07-26 12:21:07)

Railscasts - Free Ruby on Rails Screencasts

Re: Cookies in Functional Tests

Hi Ryan

thanks for the reply, yeah i wish there was a bit more consistancy in that department. As a beginner its one of those things that theres no hard and fast rule on and leaves you kind of lost. 1.2 maybe.

I understand what your saying - rails must do some magic when a request comes in with a cookie to allow you to access its value in a controller with cookie[:key]. Will add in the .values.first to my test code.

Thanks again
Garrett