Ruby on rails 如何修复轮询时的错误?

Ruby on rails 如何修复轮询时的错误?,ruby-on-rails,ruby,rspec,cucumber,long-polling,Ruby On Rails,Ruby,Rspec,Cucumber,Long 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) po

我有助手
登录
,可以登录用户。我正在尝试使用一种新方法来确保用户使用轮询登录:

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)
问题是,我的一些测试失败并出现错误:

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

我怎样才能解决这个问题?

你不需要做这些

水豚

你的
页面测试应该有内容
只需要多一点时间,你可以在步骤中或作为常规设置进行测试。默认等待时间为2秒,您可能需要5秒或更长时间

添加
Capybara.default\u wait\u time=5

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


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

但这会更改每个命令的等待时间,而不仅仅是失败的命令。在这种情况下,您需要更改它,然后在断言后恢复原始时间。在大多数情况下,我们就是这样做的,这样我们可以降低全局等待时间。这也是我们的测试所做的。应该有一个函数来实现这一点。我看到一些代码,其中使用了
wait:x
参数。但这似乎不是标准的。。。