RSpec.shared_context "wait_for_expect" do # Waits until the given expectations are all true. This function executes the given block, # and if a failure occurs it will be retried `retry_count` times before finally failing. # This is useful to expect against asynchronous/eventually consistent systems. # # @param retry_count [Integer] The total amount of times to retry the given expectation # @param sleep_duration [Integer] The total amount of time to sleep before trying again def wait_for_expect(retry_count = 40, sleep_duration = 0.5) failure_count = 0 begin yield rescue RSpec::Expectations::ExpectationNotMetError failure_count += 1 if failure_count < retry_count sleep sleep_duration retry else raise end end end end