How do I fix my Cucumber expectation error when po

2019-03-03 08:18发布

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

And here is 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)

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

How can I fix that?

1条回答
Deceive 欺骗
2楼-- · 2019-03-03 09:01

You shouldn't need to do any of that.

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

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.

Add Capybara.default_wait_time = 5

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

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.

查看更多
登录 后发表回答