2017-04-02 82 views
0

我的設計用戶是:confirmable,我想測試用戶在註冊後重定向到設計登錄頁面。它在我手動測試時正常工作,但在rspec/capybara功能規範中失敗。我正在按照these指示設置重定向。測試設計after_inactive_sign_up_path_for確認重定向

# registrations_controller.rb 
class RegistrationsController < Devise::RegistrationsController 
    protected 

    def after_inactive_sign_up_path_for(_resource) 
     new_user_session_path # redirect to login 
    end 
end 



# registration_spec.rb 
RSpec.describe 'registration process', type: feature do 
    it 'works' do 
     visit new_user_registration_path 
     fill_in 'Username', with: 'user123' 
     fill_in 'Email', with: '[email protected]' 
     fill_in 'Password', with: 'password123' 
     fill_in 'Password confirmation', with: 'password123' 
     click_on 'Sign up' 
     expect(User.find_by(username: 'user123')).not_to be_nil # sanity check 
     expect(page).to have_current_path(new_user_session_path) # failure here 
    end 
end 

故障恢復:

Failure/Error: expect(page).to have_current_path(new_user_session_path) 
     expected "/users" to equal "https://stackoverflow.com/users/sign_in" 
    # ./spec/features/registration_spec.rb:19:in `block (2 levels) in <top (required)>' 

好像page被卡在後路徑,或者被重定向到/users不正確?我知道表單接受輸入是因爲我的理智檢查用戶是否存在。

回答

0

很可能是您的用戶創建失敗,無論是由於現有用戶還是密碼不符合最低安全要求等,我不確定。在點擊之後輸出page.html會顯示任何驗證錯誤,或者您的test.log也應該顯示它們。

此外,click_on可以是異步的(當使用支持JS的驅動程序時),因此您應該交換測試的最後兩行,因爲has_current_path將等待發生頁面更改,這意味着用戶創建已完成。這不會解決當前的問題,但使用水豚匹配器確保在運行直接數據庫查詢之前已完成的操作將會減少將來的潛在風險(然而,請注意,直接數據庫查詢通常是不好的代碼異味功能測試)。