Skip to content

Instantly share code, notes, and snippets.

@blatyo
Last active December 16, 2015 02:29
Show Gist options
  • Select an option

  • Save blatyo/5363393 to your computer and use it in GitHub Desktop.

Select an option

Save blatyo/5363393 to your computer and use it in GitHub Desktop.
In order to avoid sleeping for long periods of time, switch to a frequent polling mechanism, so the success case is fast.
expect{current_path}.to eventually_eql('/')
expect{current_path}.to eventually_match(%r{/boxes/PS\d+/shipping_information/new})
expect{get_me_the_cookie('foo')[:value]}.to eventually_eql('bar')
require "timeout"
module PollingMatchers
extend RSpec::Matchers::DSL
def eventually(actual_block, operator, expected)
Timeout.timeout(Capybara.default_wait_time) do
sleep(0.1) until actual_block.call.send(operator, expected)
true
end
rescue TimeoutError
false
end
matcher :eventually_eql do |expected|
match do |block|
eventually(block, :==, expected)
end
end
matcher :eventually_match do |expected_regex|
match do |block|
eventually(block, :=~, expected_regex)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment