Class: GrpcMock::RequestStub

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

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



11
12
13
14
# File 'lib/grpc_mock/request_stub.rb', line 11

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

Instance Method Details

#match?(path, request) ⇒ Bool

Parameters:

  • path (String)
  • request (Object)

Returns:

  • (Bool)


51
52
53
# File 'lib/grpc_mock/request_stub.rb', line 51

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

#responseObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/grpc_mock/request_stub.rb', line 34

def response
  if @response_sequence.empty?
    raise GrpcMock::NoResponseError, 'Must be set some values by using #GrpMock::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



28
29
30
31
32
# File 'lib/grpc_mock/request_stub.rb', line 28

def to_raise(*exceptions)
  responses = [*exceptions].flatten.map { |e| Response::ExceptionValue.new(e) }
  @response_sequence << GrpcMock::ResponsesSequence.new(responses)
  self
end

#to_return(*values, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/grpc_mock/request_stub.rb', line 21

def to_return(*values, &block)
  responses = [*values].flatten.map { |v| Response::Value.new(v) }
  responses << Response::BlockValue.new(block) if block
  @response_sequence << GrpcMock::ResponsesSequence.new(responses)
  self
end

#with(request = nil, &block) ⇒ Object



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

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