# Cookie inspection in Rspec and Capybara

**URL:** <https://www.ruby-forum.com/t/cookie-inspection-in-rspec-and-capybara/228411>\
**Category:** RSpec\
**Created:** [April 21, 2013, 9:25am UTC](https://www.ruby-forum.com/t/cookie-inspection-in-rspec-and-capybara/228411 "2013-04-21T09:25:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![assafshomer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@assafshomer](https://www.ruby-forum.com/u/assafshomer)\
**Post date:** [April 21, 2013, 9:25am UTC](https://www.ruby-forum.com/t/cookie-inspection-in-rspec-and-capybara/228411/1 "2013-04-21T09:25:49Z")

</div>

Hi.  
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 }  
end

which fails as follows:

1. UsersController specify and controller variables  
Failure/Error: specify { cookies.should be\_nil }  
expected: nil  
got: #\<ActionDispatch::Cookies::CookieJar:0x0000000381f670  
@secret=“1a5f7c9bbff386831db09dfdfc3a4c6b820e3ae8ddf210cc246d0df4cde6492e7c05132eca91bbe0a56b5cf9b266367b834f016c899d1bf6c5ef1f6cdbf78de5”,  
@set\_cookies={“remember\_token”=\>{:value=\>“odLMhlC4aXMmRGw62naMcA”,  
:expires=\>Thu, 21 Apr 2033 07:04:12 UTC +00:00, :path=\>"/"}},  
@delete\_cookies={}, @host=“test.host”, @secure=false, @closed=false,  
@cookies={“remember\_token”=\>“odLMhlC4aXMmRGw62naMcA”},  
@permanent=#\<ActionDispatch::Cookies::PermanentCookieJar:0x00000003828d10  
@parent\_jar=#\<ActionDispatch::Cookies::CookieJar:0x0000000381f670 …\>,  
@secret=“1a5f7c9bbff386831db09dfdfc3a4c6b820e3ae8ddf210cc246d0df4cde6492e7c05132eca91bbe0a56b5cf9b266367b834f016c899d1bf6c5ef1f6cdbf78de5”\>\>
# ./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

@set\_cookies={“remember\_token”=\>{:value=\>“odLMhlC4aXMmRGw62naMcA”,  
: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/}  
end

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!

–Assaf.
