如何测试cookie到期在rails rspec [英] how do i test cookie expiry in rails rspec

查看:121
本文介绍了如何测试cookie到期在rails rspec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rspec中设置Cookie有很多困惑
http://relishapp.com/rspec/rspec-rails/v/2-6/dir/controller-specs/file/cookies



在你的控制器中,通常你可以写

  cookies ['transaction_code'] = {:expires => 300.seconds.from_now,:value => c} 

但在rspec i中只能写

  request.cookies ['transaction_code'] = transaction_code 

如果我说

  request.cookies ['transaction_code'] = {:expires => 300.seconds.from_now,:value => c} 



我将控制器中的Cookie ['transaction_code' / p>

现在我的问题是:我如何设置/测试cookie过期然后在rspec控制器测试示例?



更新:On seconds think:我的意思是:我如何测试控制器是否对预期的过期Cookie做出反应,但实际上过期的Cookie只是像没有cookie,如果我信任cookie实现,我应该做,所以也许我的问题没有意义。如果是这样,我需要测试(另一个)控制器动作是否正确设置了过期的cookie,但如果测试中的cookie ['transaction_code']只返回值,我该如何做?

解决方案

浏览器不要将Cookie属性发送回服务器。这是为什么你只能发送键值对的动作。



因为你可以假设Rails,Rack和浏览器做正确的事情与参数,所有你真的需要测试是你的代码传递给 CookieJar 的参数。



要测试在设置Cookie 的控制器中正确设置到期日期,您可以将 #cookies 方法,并确保正确的设置被传递给它。

 #app / controllers / widget_controller.rb 
...
def index
[:expiring_cookie] = {:value => 我们看到或似乎...,
:expires => 1.hour.from_now}
end
...

#spec / controllers / widget_controller_spec.rb
...
it设置Cookie do
get:index
response.cookies ['expiring_cookie']。应该eq('我们看到的或看起来...')
#只是梦中的梦想。
# - Edgar Allan Poe
end

it设置cookie过期do
stub_cookie_jar = HashWithIndifferentAccess.new
controller.stub(:cookies) {stub_cookie_jar}

get:index
expiring_cookie = stub_cookie_jar ['expiring_cookie']
expiring_cookie [:expires] .to_i.should be_within(1).of(1.hour。 from_now.to_i)
end
...

是沸腾的海洋。在某些时候,你必须假设你所在的堆栈(例如Rails,Rack,Web服务器,TCP / IP,操作系统,Web浏览器等)工作正常,并专注于你控制的代码。


There is a lot of confusion about setting cookies in rspec http://relishapp.com/rspec/rspec-rails/v/2-6/dir/controller-specs/file/cookies

in your controller, normally you can write

cookies['transaction_code'] = { :expires => 300.seconds.from_now, :value => c }

but in rspec i can only write

request.cookies['transaction_code'] = transaction_code

if i say

request.cookies['transaction_code'] = { :expires => 300.seconds.from_now, :value => c }

i get the hash back as value of cookies['transaction_code'] in my controller.

Now my question is: how do i set/test cookie expiry then in an rspec controller test example?

UPDATE: On seconds thought: What i mean is: how do i test if the controller is reacts to an expired cookie as expected, but in fact an expired cookie is just like no cookie if i trust cookie implementation, which i should do, so after all maybe my question makes no sense. If this is the case, i need to test if (another) controller action sets an expiring cookie correctly, but how do i do it if cookies['transaction_code'] in the test only gives the value back?

解决方案

Browsers do not send cookie attributes back to the server. This is why you can only send the key-value pair to the action.

Since you can assume that Rails, Rack and browsers do the right thing with the arguments, all you really need to test is the arguments your code is passing to the CookieJar.

To test that expiry is being set properly in the controller setting the cookie, you could stub out the #cookies method and make sure the right settings are being passed to it.

# app/controllers/widget_controller.rb
...
def index
    cookies[:expiring_cookie] = { :value   => 'All that we see or seem...', 
                                  :expires => 1.hour.from_now }
end
...

# spec/controllers/widget_controller_spec.rb
...
it "sets the cookie" do
  get :index
  response.cookies['expiring_cookie'].should eq('All that we see or seem...')
                                               # is but a dream within a dream.
                                               #                - Edgar Allan Poe
end

it "sets the cookie expiration" do
  stub_cookie_jar = HashWithIndifferentAccess.new
  controller.stub(:cookies) { stub_cookie_jar }

  get :index
  expiring_cookie = stub_cookie_jar['expiring_cookie']
  expiring_cookie[:expires].to_i.should be_within(1).of(1.hour.from_now.to_i)
end
...

Testing much more than this is boiling the ocean. At some point, you have to assume that the stack you are sitting on (e.g., Rails, Rack, the web server, TCP/IP, OS, web browsers, etc) works properly and focus on the code you control.

这篇关于如何测试cookie到期在rails rspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆