class ProtoPharm::RequestStub

Attributes

received_requests[R]
request_pattern[R]
response_sequence[R]

Public Class Methods

new(path) click to toggle source

@param path [String] gRPC path like /${service_name}/${method_name}

# File lib/proto_pharm/request_stub.rb, line 15
def initialize(path)
  @request_pattern = RequestPattern.new(path)
  @response_sequence = []
  @received_requests = []
end

Public Instance Methods

match?(path, request) click to toggle source

@param path [String] @param request [Object] @return [Bool]

# File lib/proto_pharm/request_stub.rb, line 71
def match?(path, request)
  @request_pattern.match?(path, request)
end
received!(request) click to toggle source
# File lib/proto_pharm/request_stub.rb, line 60
def received!(request)
  @received_requests << request
end
received_count() click to toggle source
# File lib/proto_pharm/request_stub.rb, line 64
def received_count
  received_requests.size
end
response() click to toggle source
# File lib/proto_pharm/request_stub.rb, line 46
def response
  if @response_sequence.empty?
    raise ProtoPharm::NoResponseError, "Must be set some values by using ProtoPharm::RequestStub#to_run"
  elsif @response_sequence.size == 1
    @response_sequence.first.next
  else
    if @response_sequence.first.end?
      @response_sequence.shift
    end

    @response_sequence.first.next
  end
end
to_raise(*exceptions) click to toggle source
# File lib/proto_pharm/request_stub.rb, line 40
def to_raise(*exceptions)
  responses = [*exceptions].flatten.map { |e| Response::ExceptionValue.new(e) }
  @response_sequence << ProtoPharm::ResponsesSequence.new(responses)
  self
end
to_return(*values, &block) click to toggle source
# File lib/proto_pharm/request_stub.rb, line 26
def to_return(*values, &block)
  if block_given?
    raise ArgumentError, "Cannot stub with static response if stubbing with a block" if values.any?

    responses = [ Response::Value.new(block) ]
  else
    responses = [*values].flatten.map { |v| Response::Value.new(v) }
  end

  @response_sequence << ProtoPharm::ResponsesSequence.new(responses)

  self
end
with(request = nil, &block) click to toggle source
# File lib/proto_pharm/request_stub.rb, line 21
def with(request = nil, &block)
  @request_pattern.with(request, &block)
  self
end