Rspec: setting cookies in a helper test

不问归期 提交于 2019-11-29 03:04:56

The best explanation I've been able to find is here: https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies

Quoted:

Recommended guidelines for rails-3.0.0 to 3.1.0

  • Access cookies through the request and response objects in the spec.
  • Use request.cookies before the action to set up state.
  • Use response.cookies after the action to specify outcomes.
  • Use the cookies object in the controller action.
  • Use String keys.

Example:

# spec
request.cookies['foo'] = 'bar'
get :some_action
response.cookies['foo'].should eq('modified bar')

# controller
def some_action
  cookies['foo'] = "modified #{cookies['foo']}"
end

I just stumbled upon your question (I was looking for an answer). I tried this successfully:

helper.request.cookies[:foo] = "bar"

You get the cookies the same way you set them. Example from one of my specs:

request.cookies[:email].should be nil

In a controller test this works:

@request.cookies[:cookie_name] = "cookie_value"

in a before block.

I found this here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!