Module: RSpec::Wait::Handler

Included in:
NegativeHandler, PositiveHandler
Defined in:
lib/rspec/wait/handler.rb

Overview

The RSpec::Wait::Handler module is common functionality shared between the RSpec::Wait::PositiveHandler and RSpec::Wait::NegativeHandler classes defined below. The module overrides RSpec’s handle_matcher method, allowing a block target to be repeatedly evaluated until the underlying matcher passes or the configured timeout elapses.

Instance Method Summary collapse

Instance Method Details

#handle_matcher(target, initial_matcher, message, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/wait/handler.rb', line 11

def handle_matcher(target, initial_matcher, message, &block)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  begin
    matcher = RSpec.configuration.clone_wait_matcher ? initial_matcher.clone : initial_matcher

    if matcher.respond_to?(:supports_block_expectations?) && matcher.supports_block_expectations?
      super(target, matcher, message, &block)
    else
      super(target.call, matcher, message, &block)
    end
  rescue RSpec::Expectations::ExpectationNotMetError
    raise if RSpec.world.wants_to_quit

    elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
    raise if elapsed_time > RSpec.configuration.wait_timeout

    sleep RSpec.configuration.wait_delay
    retry
  end
end