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

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

 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 

我该如何解决这个问题?

你不应该做任何这些。

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

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

添加Capybara.default_wait_time = 5

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

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