grpc_mock 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfccab3aab41c14166f2755b0c5649013ff6558a9bc9be8006cb7d9b26561455
4
- data.tar.gz: f194f1f14431a7e53b7c7189463360deda527c30d78771b4edc5796d437eaf9d
3
+ metadata.gz: 128da2bce960f7416a796a0b96bd0866a79bf30decc85e064d5c951b2f93a939
4
+ data.tar.gz: 7086f3897090c64f0d97aaa0cfbda4138787921929be89495f820dbeb866e533
5
5
  SHA512:
6
- metadata.gz: dd411e207239a64e436a1f3f85fce4e619a962bcb78d701291309ade2743e4c74aee249de2b5992f053d708c2e38feb3c88cc56cfd352ab328587d56e696bab5
7
- data.tar.gz: 2bcabc6f91b765545a4b5746100e9b5c5b1dc20e5510a4a73f04b032e8eb937ca7c06408ad2a12746c69da4e25673e64fdf6828f16337165681cf77eb9654987
6
+ metadata.gz: 23511c5cacc91af0c26eafceee5f0a986605cb44feccf169608a35ff8f353a6f0933bc8ea0af22fbb9f04f190b5791ce6b76825040cb49e9655815bdb0f7b1a1
7
+ data.tar.gz: b069f76b458a0d95c7006a9f079e2c6bcd7c600ef563355716aa15f9208f79e3262a49a3efea6e7bc09169e5e2a8d60945f6bd772683896f44323bf3fbe7f5f4
data/README.md CHANGED
@@ -30,35 +30,69 @@ require 'grpc_mock/rspec'
30
30
 
31
31
  See definition of protocol buffers and gRPC generated code in [spec/exmaples/hello](https://github.com/ganmacs/grpc_mock/tree/master/spec/examples/hello)
32
32
 
33
- ##### Stubbed request based on path and with the default response
33
+ ### Stubbed request based on path and with the default response
34
34
 
35
35
  ```ruby
36
36
  GrpcMock.stub_request("/hello.hello/Hello").to_return(Hello::HelloResponse.new(msg: 'test'))
37
37
 
38
38
  client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
39
- client = client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test')
39
+ client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test')
40
40
  ```
41
41
 
42
- ##### Stubbing requests based on path and request
42
+ ### Stubbing requests based on path and request
43
43
 
44
44
  ```ruby
45
45
  GrpcMock.stub_request("/hello.hello/Hello").with(Hello::HelloRequest.new(msg: 'hi')).to_return(Hello::HelloResponse.new(msg: 'test'))
46
46
 
47
47
  client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
48
- client = client.hello(Hello::HelloRequest.new(msg: 'hello')) # => send a request to server
49
- client = client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test') (without any requests to server)
48
+ client.hello(Hello::HelloRequest.new(msg: 'hello')) # => send a request to server
49
+ client client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test') (without any requests to server)
50
50
  ```
51
51
 
52
- ##### Real requests to network can be allowed or disabled
52
+ ### Responding dynamically to the stubbed requests
53
+
54
+ ```ruby
55
+ GrpcMock.stub_request("/hello.hello/Hello").to_return do |req|
56
+ Hello::HelloResponse.new(msg: "#{req.msg} too")
57
+ end
58
+
59
+ client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
60
+ client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'hi too')
61
+ ```
62
+
63
+ ### Real requests to network can be allowed or disabled
53
64
 
54
65
  ```ruby
55
66
  client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
56
67
 
57
68
  GrpcMock.disable_net_connect!
58
- client = client.hello(Hello::HelloRequest.new(msg: 'hello')) # => Raise NetConnectNotAllowedError error
69
+ client.hello(Hello::HelloRequest.new(msg: 'hello')) # => Raise NetConnectNotAllowedError error
59
70
 
60
71
  GrpcMock.allow_net_connect!
61
- client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure) # => send a request to server
72
+ Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure) # => send a request to server
73
+ ```
74
+
75
+ ### Raising errors
76
+
77
+ **Exception declared by class**
78
+
79
+ ```ruby
80
+ GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError)
81
+
82
+ client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
83
+ client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Raise StandardError
84
+ ```
85
+
86
+ **or by exception instance**
87
+
88
+ ```ruby
89
+ GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError.new("Some error"))
90
+ ```
91
+
92
+ **or by string**
93
+
94
+ ```ruby
95
+ GrpcMock.stub_request("/hello.hello/Hello").to_raise("Some error")
62
96
  ```
63
97
 
64
98
  ## Contributing
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'bundler'
28
28
  spec.add_development_dependency 'grpc-tools'
29
29
  spec.add_development_dependency 'pry-byebug'
30
- spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rake', '>= 12.3.3'
31
31
  spec.add_development_dependency 'rspec', '~> 3.0'
32
32
  spec.add_development_dependency 'rubocop'
33
33
  end
@@ -15,7 +15,7 @@ module GrpcMock
15
15
 
16
16
  mock = GrpcMock.stub_registry.response_for_request(method, request)
17
17
  if mock
18
- mock.evaluate
18
+ mock.evaluate(request)
19
19
  elsif GrpcMock.config.allow_net_connect
20
20
  super
21
21
  else
@@ -32,7 +32,7 @@ module GrpcMock
32
32
  r = requests.to_a # FIXME: this may not work
33
33
  mock = GrpcMock.stub_registry.response_for_request(method, r)
34
34
  if mock
35
- mock.evaluate
35
+ mock.evaluate(r)
36
36
  elsif GrpcMock.config.allow_net_connect
37
37
  super
38
38
  else
@@ -47,7 +47,7 @@ module GrpcMock
47
47
 
48
48
  mock = GrpcMock.stub_registry.response_for_request(method, request)
49
49
  if mock
50
- mock.evaluate
50
+ mock.evaluate(request)
51
51
  elsif GrpcMock.config.allow_net_connect
52
52
  super
53
53
  else
@@ -63,7 +63,7 @@ module GrpcMock
63
63
  r = requests.to_a # FIXME: this may not work
64
64
  mock = GrpcMock.stub_registry.response_for_request(method, r)
65
65
  if mock
66
- mock.evaluate
66
+ mock.evaluate(r)
67
67
  elsif GrpcMock.config.allow_net_connect
68
68
  super
69
69
  else
@@ -18,8 +18,9 @@ module GrpcMock
18
18
  self
19
19
  end
20
20
 
21
- def to_return(*values)
21
+ def to_return(*values, &block)
22
22
  responses = [*values].flatten.map { |v| Response::Value.new(v) }
23
+ responses << Response::BlockValue.new(block) if block
23
24
  @response_sequence << GrpcMock::ResponsesSequence.new(responses)
24
25
  self
25
26
  end
@@ -16,7 +16,7 @@ module GrpcMock
16
16
  end
17
17
  end
18
18
 
19
- def evaluate
19
+ def evaluate(_request = nil)
20
20
  raise @exception.dup
21
21
  end
22
22
  end
@@ -26,9 +26,19 @@ module GrpcMock
26
26
  @value = value
27
27
  end
28
28
 
29
- def evaluate
29
+ def evaluate(_request = nil)
30
30
  @value.dup
31
31
  end
32
32
  end
33
+
34
+ class BlockValue
35
+ def initialize(block)
36
+ @block = block
37
+ end
38
+
39
+ def evaluate(request)
40
+ @block.call(request)
41
+ end
42
+ end
33
43
  end
34
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrpcMock
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grpc_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Iwama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-17 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grpc
@@ -76,16 +76,16 @@ dependencies:
76
76
  name: rake
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - "~>"
79
+ - - ">="
80
80
  - !ruby/object:Gem::Version
81
- version: '10.0'
81
+ version: 12.3.3
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
- - - "~>"
86
+ - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: '10.0'
88
+ version: 12.3.3
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: rspec
91
91
  requirement: !ruby/object:Gem::Requirement