簡體   English   中英

使用Capybara登錄路徑后如何測試設計

[英]How to test devise after sign in path with Capybara

我正在嘗試測試通過Devise gem登錄后會發生什么。 例如,在用戶成功登錄后,我可以讓控制器轉到student_dashboard_path。

如何使用Capybara和Rspec進行測試?

我目前在:

/spec/features/user_signs_in_sees_dashboard_spec.rb

require 'rails_helper'

feature 'User sign in' do
  scenario 'successfully from sign in page and sees student dashboard' do
     sign_in
     visit student_dashboard_path
     expect(current_path).to eq(student_dashboard_path)
  end
end

而我在:

/spec/support/features/sign_in.rb

module Features
  def sign_in
    visit user_session_path
    fill_in 'Email', with: User.first.email
    fill_in 'Password', with: User.first.password
    click_button 'Log in'
  end
end

我收到此錯誤消息:

  1) User signs in successfully from sign in and sees dashboard
 Failure/Error: expect(current_path).to eq(student_dashboard_path)

   expected: "/student/dashboard"
        got: "/student/login"

我不確定為什么無法登錄並查看學生儀表板。

我將下面的原始答案留給仍在Capybara <2.5.0以下的任何人,但是在2.5.0中,您現在可以

expect(page).to have_current_path(<expected path>)

它會在檢查路徑時使用Capybara的等待行為

----以下僅適用於<2.5.0的Capybara版本

Expect(current_path).to eq(...)並不等待路徑更改,它只是與調用時的當前路徑進行比較。 如果您在單擊按鈕后睡了,我敢打賭,它可以工作。 更好的解決方案是使用類似

expect(page).to have_text('You are now logged in')

在click_button之后。 這將導致水豚等待登錄完成,頁面加載(並且出現登錄通知),並因此直到current_path也更改為止。

使用中間的save_and_open_page來確定是否正確設置了字段。 如果您在同一台計算機上工作,則可以切換到以在真實的瀏覽器上進行測試。

另外請確保此代碼不需要任何JS即可工作,因為默認的水豚匹配器將無法運行它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM