2017-04-02 39 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将等待发生页面更改,这意味着用户创建已完成。这不会解决当前的问题,但使用水豚匹配器确保在运行直接数据库查询之前已完成的操作将会减少将来的潜在风险(然而,请注意,直接数据库查询通常是不好的代码异味功能测试)。