Matt Baker
Matt Baker

Reputation: 3754

How do I access cookies in a helper spec?

I can't figure out how to test that a cookie has been set when testing my helper method.

Hypothetical helper method:

def my_helper(k,v)
    cookies[k] = v
end

Test:

it 'should set cookies' do  
  helper.my_helper("foo", "bar")
  helper.cookies["foo"].should == "bar" #nil
  helper.response.cookies["foo"].should == "bar" #nil
end

Anyone know how to do this?

Upvotes: 5

Views: 2126

Answers (3)

jaredjacobs
jaredjacobs

Reputation: 6165

Substituting a simple rspec mock for the CookieJar works, if you're willing to:

helper.stubs(:cookies => cookies = mock)
cookies.expects(:[]=).with('foo', 'bar')
helper.my_helper('foo', 'bar')

Upvotes: 4

Dty
Dty

Reputation: 12273

I'm on rails 3.2 and rspec 2.8. Despite what the rspec docs says the following works for me in a request spec (ie. integration test).

it 'should set cookies' do  
  cookies['foo'] = 'bar'
  visit "/"
  cookies['foo'].should == 'bar'
end

Upvotes: 2

Rodrigo Zurek
Rodrigo Zurek

Reputation: 4575

request the cookie through

 helper.request.cookies[:awesome] = "something"

Upvotes: 0

Related Questions