Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 56 lines (45 sloc) 1.727 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
module WebMock
  class RequestExecutionVerifier

    attr_accessor :request_pattern, :expected_times_executed, :times_executed

    def initialize(request_pattern = nil, expected_times_executed = nil)
      @request_pattern = request_pattern
      @expected_times_executed = expected_times_executed
    end

    def matches?
      @times_executed =
      RequestRegistry.instance.times_executed(@request_pattern)
      @times_executed == (@expected_times_executed || 1)
    end

    def does_not_match?
      @times_executed =
      RequestRegistry.instance.times_executed(@request_pattern)
      if @expected_times_executed
        @times_executed != @expected_times_executed
      else
        @times_executed == 0
      end
    end


    def failure_message
      expected_times_executed = @expected_times_executed || 1
      text = %Q(The request #{request_pattern.to_s} was expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
      text << self.class.executed_requests_message
      text
    end

    def negative_failure_message
      text = if @expected_times_executed
        %Q(The request #{request_pattern.to_s} was not expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
      else
        %Q(The request #{request_pattern.to_s} was expected to execute 0 times but it executed #{times(times_executed)})
      end
      text << self.class.executed_requests_message
      text
    end

    def self.executed_requests_message
      "\n\nThe following requests were made:\n\n#{RequestRegistry.instance.to_s}\n" + "="*60
    end

    private

    def times(times)
      "#{times} time#{ (times == 1) ? '' : 's'}"
    end

  end
end
Something went wrong with that request. Please try again.