[英]Devise feature tests not redirecting to sign_in page
我僅在功能測試中注意到一些奇怪的行為,即當用戶未登錄並且他們沒有被重定向到登錄頁面時,我試圖理解原因。 我正在使用poltergeist作為javascript驅動程序。
這里有幾個例子:
# works as expected
it 'redirects to sign_in page when not logged in' do
get :index, {}
expect(response).to eq('/users/sign_in')
end
# does not redirect to '/users/sign_in'
it 'should redirect to the user sign in page' do
visit '/'
expect(page.current_path).to redirect_to('/users/sign_in')
end
有誰知道為什么devise在功能測試中沒有重定向到sign_in頁面?
您不能在Capybara中使用redirect_to-Capybara不提供有關頁面是否正在重定向的數據,它僅以字符串形式提供其所在的URL。 你可能想要
expect(page).to have_current_path('/users/sign_in')
注意:嘗試檢查同一件事的幼稚方法是將expect(page.current_path).to eq('/users/sign_in')
但是會立即檢查current_path,與JS一起使用時會導致expect(page.current_path).to eq('/users/sign_in')
測試有能力的驅動程序,因為這些驅動程序中的大多數動作可以異步發生。 使用have_current_path
匹配器會增加等待/重have_current_path
為,因此是正確的使用方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.