Sha256: b7ed133a59fdacae818611c6756311ff5f4a581c8dc3c70535b4235f8ee15c88

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module WebMock
  class RequestStub

    attr_accessor :request_profile, :responses

    def initialize(method, uri)
      @request_profile = RequestProfile.new(method, uri)
      @responses_sequences = []
      self
    end

    def with(params = {}, &block)
      @request_profile.with(params, &block)
      self
    end

    def to_return(*response_hashes)
      @responses_sequences << ResponsesSequence.new([*response_hashes].flatten.map {|r| WebMock::Response.new(r)})
      self
    end

    def to_raise(*exceptions)
      @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e| WebMock::Response.new(:exception => e)})
      self
    end

    def response
      if @responses_sequences.empty?
        WebMock::Response.new
      elsif @responses_sequences.length > 1
        @responses_sequences.shift if @responses_sequences.first.end?
        @responses_sequences.first.next_response
      else
        @responses_sequences[0].next_response
      end
    end

    def then
      self
    end

    def times(number)
      raise "times(N) accepts integers >= 1 only" if !number.is_a?(Fixnum) || number < 1
      if @responses_sequences.empty?
        raise "Invalid WebMock stub declaration." +
          " times(N) can be declared only after response declaration."
      end
      @responses_sequences.last.times_to_repeat += number-1
      self
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webmock-0.9.1 lib/webmock/request_stub.rb
webmock-0.9.0 lib/webmock/request_stub.rb