【问题标题】:How to fix a flaky Capybara test for the Devise password reset flow如何修复设计密码重置流程的不稳定 Capybara 测试
【发布时间】:2021-05-17 18:40:56
【问题描述】:

我在 Rails 中有一个不稳定的系统测试,我似乎无法修复。每次这个测试最初开始失败时,我都会在 capybara 的 using_wait_time 方法上增加几秒钟,测试会通过一段时间。通常,这个测试会单独通过,然后在我运行整个测试套件时失败。然后我增加了等待时间,在运行整个测试套件时它会经过一段时间,直到我在我的应用程序中更改了一些其他内容,无论出于何种原因破坏了这个特定的测试。

一开始我以为,好吧,我只需要等待浏览器,这是一个系统测试,但现在这个等待时间已经开始爬到一个荒谬的程度。 (旁注:在搜索栏中输入搜索词并等待响应的系统测试也发生了类似的事情。)有没有更好的测试方法或告诉 Capybara 等待浏览器的唯一方法?我知道我可以信任 Devise,不需要对 Devise 的密码重置邮件进行单元测试,但我想测试用户在密码重置过程中的流程。

这是不稳定的测试:

require 'application_system_test_case'

class UserPasswordResetTest < ApplicationSystemTestCase
  def setup
    ActionMailer::Base.deliveries.clear
    @user = users(:george)
  end

  test "should allow you to request a password reset email" do
    visit login_path
    click_link "Forgot your password?"
    assert_current_path "/users/password/new"
    using_wait_time(30) do
      fill_in "user[email]", with: @user.email
    end
    click_button "Request password reset"
    using_wait_time(25) do
      assert_text "You will receive an email with instructions on how to reset your password in a few minutes."
    end
  end
end

这是我的测试助手:

test/application_system_test_helper.rb

test/test_helper.rb

有什么更好的方法来测试这个不那么不稳定的密码重置流程?

【问题讨论】:

    标签: ruby-on-rails ruby devise capybara minitest


    【解决方案1】:

    如果测试通过了延长等待,那么您需要查看测试日志以了解为什么您的控制器操作需要这么长时间才能呈现(Capybara 只是在等待该内容出现在浏览器中,然后继续尽快)。如果测试无法通过延长等待可靠地通过,那么您需要提供有关返回的确切错误的详细信息。

    另请注意,当您只调整单个调用的最大等待时间时,您不需要使用 using_wait_time 方法,您只需将 wait 传递给您正在调用的方法

    fill_in "user[email]", with: @user.email, wait: 30
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多