轮询时如何修复我的

轮询时如何修复我的

本文介绍了轮询时如何修复我的 Cucumber 期望错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有帮助程序 sign_in 来登录用户.我正在尝试使用一种新方法来确保用户使用轮询登录:

I have the helper sign_in which signs in a user. I'm trying to use a new approach to make sure that the user signed in using polling:

def sign_in(user, password = '111111')
  # ...
  click_button 'sign-in-btn'

  eventually(5){ page.should have_content user.username.upcase }
end

这里是最终:

module AsyncSupport
  def eventually(timeout = 2)
    polling_interval = 0.1
    time_limit = Time.now + timeout

    loop do
      begin
        yield
        rescue Exception => error
      end
      return if error.nil?
      raise error if Time.now >= time_limit
      sleep polling_interval
    end
  end
end

World(AsyncSupport)

问题是我的一些测试失败并出现错误:

The problem is that some of my tests fail with an error:

expected to find text "USER_EMAIL_1" in "{"success":true,"redirect_url":"/users/1/edit"}" (RSpec::Expectations::ExpectationNotMetError)
./features/support/spec_helper.rb:25:in `block in sign_in'
./features/support/async_support.rb:8:in `block in eventually'
./features/support/async_support.rb:6:in `loop'
./features/support/async_support.rb:6:in `eventually'
./features/support/spec_helper.rb:23:in `sign_in'
./features/step_definitions/user.steps.rb:75:in `/^I am logged in as a "([^"]*)"$/'
features/user/edit.feature:8:in `And I am logged in as a "user"'

Failing Scenarios:
cucumber features/user/edit.feature:6 # Scenario: Editing personal data

我该如何解决?

推荐答案

您不应该需要做任何这些.

You shouldn't need to do any of that.

Capybara 具有 强大的同步功能意味着您无需手动等待异步进程完成

Capybara has Powerful synchronization features mean you never have to manually wait for asynchronous processes to complete

您对 page.should have_content 的测试只需要多一点时间,您可以在步骤中或作为一般设置提供它.默认等待时间为 2 秒,您可能需要 5 秒或更长时间.

Your test for page.should have_content just needs a bit more time, you can give it to it in the step or as a general setup. The default wait is 2 seconds, and you might need 5 seconds or more.

添加Capybara.default_wait_time = 5

在上面的链接中,向下搜索并找到异步 JavaScript(Ajax 和朋友)

In the link above, search down and find Asynchronous JavaScript (Ajax and friends)

您应该能够完全删除您的 AsyncSupport.请记住,如果您将其设置在一个步骤中并且您希望等待时间为 2 秒,否则您可能需要一个 ensure 块将其设置回原始时间.

You should be able to delete your AsyncSupport entirely. Just remember, if you set this inside a step and you want the wait to be 2 seconds otherwise, you might want an ensure block to set it back to the original time.

这篇关于轮询时如何修复我的 Cucumber 期望错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 22:29