轮询时如何修复黄瓜期望错误?

时间:2013-11-11 15:23:59

标签: ruby-on-rails ruby rspec cucumber long-polling

我有一个帮助用户sign_in。我尝试使用新方法确保用户使用轮询登录:

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

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

这是eventually

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

我该如何解决?

1 个答案:

答案 0 :(得分:1)

你不应该做任何这些。

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

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

添加Capybara.default_wait_time = 5

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

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