title: How do I fix my Cucumber expectation error when polling?
body: <p>I have the helper <code>sign_in</code> which signs in a user. I'm trying to use a new approach to make sure that the user signed in using polling: </p>
  
  <pre><code>def sign_in(user, password = '111111')
    # ...
    click_button 'sign-in-btn'
  
    eventually(5){ page.should have_content user.username.upcase }
  end
  </code></pre>
  
  <p>And here is <code>eventually</code>:</p>
  
  <pre><code>module AsyncSupport
    def eventually(timeout = 2)
      polling_interval = 0.1
      time_limit = Time.now + timeout
  
      loop do
        begin
          yield
          rescue Exception =&gt; error
        end
        return if error.nil?
        raise error if Time.now &gt;= time_limit
        sleep polling_interval
      end
    end
  end
  
  World(AsyncSupport)
  </code></pre>
  
  <p>The problem is that some of my tests fail with an error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>How can I fix that?</p>