【问题标题】:Test devise after_inactive_sign_up_path_for confirmable redirect测试设计 after_inactive_sign_up_path_for 可确认重定向
【发布时间】:2017-04-02 02:56:17
【问题描述】:

我的设计用户是:confirmable,我想测试用户在注册后是否被重定向到设计登录页面。当我手动测试它时它可以工作,但在 rspec/capybara 功能规范中失败。我正在按照these 的说明设置重定向。

# 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: 'user123@email.com'
        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 "/users/sign_in"
     # ./spec/features/registration_spec.rb:19:in `block (2 levels) in <top (required)>'

page 似乎卡在帖子路径中,或者被错误地重定向到 /users?我知道表单正在接受输入,因为我检查了用户是否存在。

【问题讨论】:

    标签: ruby-on-rails rspec devise capybara devise-confirmable


    【解决方案1】:

    您的用户创建很可能失败,无论是由于现有用户还是密码不符合最低安全要求等,我不确定。单击后输出page.html 应该会显示任何验证错误,或者您的test.log 也应该显示它们。

    此外,click_on 可以是异步的(当使用支持 JS 的驱动程序时),因此您应该交换测试的最后两行,因为 have_current_path 将等待页面更改发生,这意味着用户创建已完成。这不会解决您当前的问题,但使用 Capybara 匹配器确保在运行直接数据库查询之前操作已完成将减少您将来的潜在脆弱性(但是请注意,直接数据库查询通常是一种不好的代码气味功能测试)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2016-07-22
      • 1970-01-01
      • 2017-12-30
      相关资源
      最近更新 更多