[rspec-users] Cookie inspection in Rspec and Capybara

Assaf Shomer lists at ruby-forum.com
Sun Apr 21 07:25:49 UTC 2013

I'm trying to inspect an authentication cookie inside of a test.
My goal is to test a remember me functionality, so I would like to test
for the expiration date of the set cookie.
This turns out to be extremely difficult (for me at least).

The farthest I managed to get so far is the following:

describe "cookies" do
  let(:user) { FactoryGirl.create(:user)}
  before { controller.sign_in(user) }
  specify { cookies.should be_nil }

which fails as follows:

1) UsersController specify and controller variables
     Failure/Error: specify { cookies.should be_nil }
       expected: nil
            got: #<ActionDispatch::Cookies::CookieJar:0x0000000381f670
:expires=>Thu, 21 Apr 2033 07:04:12 UTC +00:00, :path=>"/"}},
@delete_cookies={}, @host="test.host", @secure=false, @closed=false,
@parent_jar=#<ActionDispatch::Cookies::CookieJar:0x0000000381f670 ...>,
     # ./spec/controllers/users_controller_spec.rb:212:in `block (3
levels) in <top (required)>'

As you can see the information I'm interested in is buried inside this
'cookies' object in an instance variable @set_cookies

:expires=>Thu, 21 Apr 2033 07:04:12 UTC +00:00, :path=>"/"}

However, so far I am unable to extract the relevant data (i.e. the
'expires') out of this object.
How does one do that?

I read somewhere online that the cookies are buried inside the
'response.headers' object, but at least inside the users controller this
object is an empty hash.

 describe "cookies" do
   before { get user_path(user) }
   specify {response.headers["Set-Cookie"].should =~ /remember_token/}

Interestingly enough the same test as an integration test (under the
spec directory) does yield a non empty hash but the cookie i'm looking
for is not there, only the session cookie.

So there it is, what seems to me like a pretty elementary check turns
out to be very hard to implement in Rspec.
I find that hard to believe and thus expect that this is just a
testimony of my poor coding abilities.

Can anyone please shed some light on this issue?
What am I doing wrong here?
How does one test for cookies' 'expire' in Rspec?
How can I extract the 'expire' data from the cookie object above?

Thanks a lot!


Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list