Class: ProtoPharm::RequestStub

Inherits:
Object
  • Object
show all
Defined in:
lib/proto_pharm/request_stub.rb

Direct Known Subclasses

ActionStub

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ RequestStub

Returns a new instance of RequestStub.

Parameters:

  • path (String)

    gRPC path like /$service_name/$method_name



15
16
17
18
19
# File 'lib/proto_pharm/request_stub.rb', line 15

def initialize(path)
  @request_pattern = RequestPattern.new(path)
  @response_sequence = []
  @received_requests = []
end

Instance Attribute Details

#received_requestsObject (readonly)

Returns the value of attribute received_requests.



10
11
12
# File 'lib/proto_pharm/request_stub.rb', line 10

def received_requests
  @received_requests
end

#request_patternObject (readonly)

Returns the value of attribute request_pattern.



10
11
12
# File 'lib/proto_pharm/request_stub.rb', line 10

def request_pattern
  @request_pattern
end

#response_sequenceObject (readonly)

Returns the value of attribute response_sequence.



10
11
12
# File 'lib/proto_pharm/request_stub.rb', line 10

def response_sequence
  @response_sequence
end

Instance Method Details

#match?(path, request) ⇒ Bool

Parameters:

  • path (String)
  • request (Object)

Returns:

  • (Bool)


71
72
73
# File 'lib/proto_pharm/request_stub.rb', line 71

def match?(path, request)
  @request_pattern.match?(path, request)
end

#received!(request) ⇒ Object



60
61
62
# File 'lib/proto_pharm/request_stub.rb', line 60

def received!(request)
  @received_requests << request
end

#received_countObject



64
65
66
# File 'lib/proto_pharm/request_stub.rb', line 64

def received_count
  received_requests.size
end

#responseObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



40
41
42
43
44
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



21
22
23
24
# File 'lib/proto_pharm/request_stub.rb', line 21

def with(request = nil, &block)
  @request_pattern.with(request, &block)
  self
end