[英]Testing Devise session timeout with RSpec
我想自定义我的Rails应用程序将自动结束用户会话的时间段。
我发现这个问题给了我我需要的东西,但是我想通过TDD来解决这个问题。 我有一个相对坚固的应用程序测试套件,我只是有点迷失测试用户的会话是否已经过期,除了测试坐在那里等待会话在x.minutes
后x.minutes
已经过去了。 当然有更好的方法。
我检查了Devise和Warden的文档,找到了处理这个问题的测试助手。 有人有任何提示吗?
如何将user.timedout?
存根user.timedout?
归还真的吗? 或者,您可以在测试中传递超过自定义超时值的自定义时间,例如user.timed_out?(31.minutes.ago)
。
而不是玩精确的时间过去:
由于Devise是基于Warden构建的,因此您可以模拟Devise :: SessionsController通过Warden :: Test :: Helpers超时用户,可以访问Rack代理。
# settings_page_spec.rb
include Warden::Test::Helpers
it 'does not allow updating password after timed out' do
expect(current_path).to eq(user_settings_path(user))
clink_link 'Manage Password'
expect(page).to have_button('Update Password')
Warden.on_next_request do |proxy|
proxy.set_user(nil)
click_button 'Update Password'
expect(current_path).to eq(login_path)
end
end
完整的博客文章: http : //www.morphogenic.net/2016/10/devise-test-timeout-user-in-featureintegration-specs-using-warden/
默认设备设置超时为30分钟。 你可以用user.timedout?([specify time in past])
例如:
user.timedout?(30.minutes.ago)
将返回false
user.timedout?(10.minutes.ago)
将返回true
你可以检查rspec里面的user.timedout?()
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.