Skip to content

Instantly share code, notes, and snippets.

@rockwood
Created April 1, 2015 16:09
Show Gist options
  • Save rockwood/ea59c05b2378f5e73bb9 to your computer and use it in GitHub Desktop.
Save rockwood/ea59c05b2378f5e73bb9 to your computer and use it in GitHub Desktop.
RSpec 3 Async Helpers
# Taken from https://gist.github.com/mattwynne/1228927
#
# usage:
# it "should return a result of 5" do
# eventually { long_running_thing.result.should eq(5) }
# end
#
module AsyncHelpers
def eventually(options = {})
timeout = options[:timeout] || 2
interval = options[:interval] || 0.1
time_limit = Time.now + timeout
loop do
begin
result = yield
rescue RSpec::Expectations::ExpectationNotMetError, StandardError => error
end
return result if error.nil?
raise error if Time.now >= time_limit
sleep interval
end
end
end
RSpec.configure do |config|
config.include AsyncHelpers
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment