[英]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.