【问题标题】:Rails Password Reset Test Passes When Run Individually But Fails When Running the Entire Test SuiteRails 密码重置测试在单独运行时通过,但在运行整个测试套件时失败
【发布时间】:2019-09-06 00:34:21
【问题描述】:

我正在测试使用 Clearance 身份验证 gem 的密码重置流程,但我不知道如何为此编写测试,以便在我运行整个测试套件时通过。当我单独运行它时测试通过,但当我运行整个测试套件时失败,给我这个错误消息:

Capybara::ExpectationNotMet: expected "/users/467591525/password/edit?token=8fd60112cf461061eb405632f35e08a3830f661c" to equal "/users/467591525/password/edit"

我最初尝试将其作为系统测试进行,这是我进行所有其他类似测试的地方。那是在test/system/user_password_reset_test.rb 中,它从ApplicationSystemTestCase 继承为class UserPasswordResetTest < ApplicationSystemTestCase。

但是,当我将该测试作为系统测试单独运行时,我会收到相同的错误消息。

如果我将其切换到带有 class PasswordResetTest < ActionDispatch::IntegrationTest 的集成测试,则单独运行时测试通过,但在运行整个测试套件时失败并出现相同的 Capybara::ExpectationNotMet 错误。

这是完整的测试:

require 'application_system_test_case'

class PasswordResetTest < ActionDispatch::IntegrationTest

  def setup
    clear_emails
    @user = users(:lee)
  end

  test 'User can reset their password' do
    perform_enqueued_jobs do
      visit sign_in_path
      assert_current_path sign_in_path
      click_link 'Forgot Password'
      assert_current_path new_password_path
      assert_title 'Password reset | Flagship'
      assert_selector 'h1', text: 'Reset your password'
      fill_in('Email', with: @user.email)
      click_button 'Reset password'
      assert_current_path passwords_path
      assert_selector 'h1', text: 'Your password reset email is on the way.'
    end
    sleep 10
    open_email(@user.email)
    current_email.click_link 'Change my password'
    assert_current_path(edit_user_password_path(@user, token: @user.confirmation_token))
    assert_title 'Change your password | Flagship'
    assert_selector 'h1', text: 'Change your password'
    fill_in('Choose password', with: 'New_Password')
    click_button 'Save this password'
    assert_current_path(dashboard_path)
  end
end

我已经解决这个问题一段时间了,无法弄清楚我做错了什么。这是我的test/application_system_test_case.rb:

需要'test_helper'

# System tests
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400]

  include SignInHelper
end

还有我的test/test_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'minitest/reporters'
Minitest::Reporters.use!

# Helpers used in tests
module SignInHelper
  def sign_in_as_user_without_account
    @user_without_account = users(:user_without_account)
    visit root_path(as: @user_without_account)
  end

  def sign_in_as_account_owner
    @account_owner = users(:lee)
    visit root_path(as: @account_owner)
  end
end

# Model tests
class ActiveSupport::TestCase
  parallelize(workers: :number_of_processors)

  fixtures :all
end


class ActionDispatch::IntegrationTest
  # Controllers
  require 'capybara/rails'
  require 'capybara/minitest'
  include Capybara::DSL
  include SignInHelper

  # Mailers
  include ActiveJob::TestHelper
  include Capybara::Email::DSL

end

是不是我在那里配置了错误?


更新

正如 Thomas 的回答所建议的那样,添加 ignore_query: true 修复了测试下降的线路。所以我将assert_current_path(edit_user_password_path(@user, token: @user.confirmation_token)) 更改为assert_current_path(edit_user_password_path(@user), ignore_query: true) 但现在测试只是在下一步失败。

似乎当水豚单击电子邮件中的链接时,它没有转到带有允许用户创建新密码的令牌的正确页面。相反,它会返回密码重置页面,并显示 Clearance flash 消息:failure_when_forbidden: Please double check the URL or try submitting the form again.。

因此,Capybara 单击链接似乎无法使用重置令牌转到正确的页面。但奇怪的是,它在单独运行时通过,然后每次运行整个测试套件时都会失败。

这是测试失败的屏幕截图:

【问题讨论】:

    标签: ruby-on-rails capybara integration-testing minitest system-testing


    【解决方案1】:

    这应该仍然是系统测试。之后,很可能一旦用户点击编辑密码路由,令牌就会被重置,因此它只能使用一次。这可能会使您的比较失败。但是,您确实不需要确认令牌是路由的一部分,因为如果令牌验证失败,您将无法进入编辑密码页面,并且其余测试将失败。因此,而不是

    assert_current_path(edit_user_password_path(@user, token: @user.confirmation_token))
    

    你可以这样做

    assert_current_path(edit_user_password_path(@user), ignore_query: true)
    

    【讨论】:

    • Thomas:刚刚找到时间回到这个问题并重新打开这个问题,因为您的答案修复了测试中的那一步,但现在测试在下一步失败了。仍然难以解决这里的潜在问题。刚刚更新了更详细的问题并添加了赏金。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多