protobuf-rspec 0.1.0.rc2 → 0.1.0.rc3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,177 +12,242 @@ module Protobuf
12
12
  module RSpec
13
13
  module Helpers
14
14
 
15
- # Call a local service to test responses and behavior based on the given request.
16
- # Should use to outside-in test a local RPC Service without testing the underlying socket implementation.
17
- #
18
- # @example Test a local service method
19
- # # Implementation
20
- # module Proto
21
- # class UserService < Protobuf::Rpc::Service
22
- # def create
23
- # user = User.create_from_proto(request)
24
- # if request.name
25
- # respond_with(ProtoRepresenter.new(user))
26
- # else
27
- # rpc_failed 'Error: name required'
28
- # end
29
- # end
30
- # end
31
- # end
32
- #
33
- # # Spec
34
- # describe Proto::UserService do
35
- # describe '#create' do
36
- # it 'creates a new user' do
37
- # create_request = Proto::UserCreate.new(...)
38
- # service = call_local_service(Proto::UserService, :create, create_request)
39
- # service.response.should eq(some_response_object)
40
- # end
41
- #
42
- # it 'fails when name is not given' do
43
- # bad_req = { :name => nil }
44
- # service = call_local_service(Proto::UserService, :create, :create_request) do |service|
45
- # # Block is yielded before the method is invoked.
46
- # service.should_receive(:rpc_failed).with('Error: name required')
47
- # end
48
- # end
49
- # end
50
- # end
51
- #
52
- # @param [Class] klass the service class constant.
53
- # @param [Symbol, String] method a symbol or string denoting the method to call.
54
- # @param [Protobuf::Message or Hash] request the request message of the expected type for the given method.
55
- # @param [block] optionally provide a block which will be yielded the service instance just prior to invoking the rpc method.
56
- # @return [Protobuf::Service] the service instance post-calling the rpc method.
57
- def call_local_service(klass, method_name, request)
58
- request = klass.rpcs[method_name].request_type.new(request) if request.is_a?(Hash)
59
- service = klass.new(method_name, request.serialize_to_string)
60
- yield(service) if block_given?
61
- service.method(method_name).call
62
- service
15
+ def self.included(other)
16
+ other.class_eval do
17
+ extend ::Protobuf::RSpec::Helpers::ClassMethods
18
+ include ::Protobuf::RSpec::Helpers::InstanceMethods
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+
24
+ # Set the service subject. Use this method when the described_class is
25
+ # not the class you wish to use with methods like local_rpc. In t
26
+ #
27
+ # @example Override subject service for local_rpc calls
28
+ # describe Foo::BarService do
29
+ # # Use Foo::BazService instead of Foo::BarService
30
+ # subject_service { Foo::BazService }
31
+ #
32
+ # subject { local_rpc(:find, request) }
33
+ # its('response.records') { should have(3).items }
34
+ # end
35
+ #
36
+ # @example Override subject service for remote_rpc mocks
37
+ # describe BarController do
38
+ # describe '#index' do
39
+ # subject_service { Foo::BarService }
40
+ # subject { remote_rpc(:find, request, response) }
41
+ # end
42
+ # end
43
+ #
44
+ def subject_service
45
+ if block_given?
46
+ @_subject_service = yield
47
+ else
48
+ defined?(@_subject_service) ? @_subject_service : described_class
49
+ end
50
+ end
51
+
63
52
  end
64
- alias_method :call_service, :call_local_service
65
-
66
- # Create a mock service that responds in the way you are expecting to aid in testing client -> service calls.
67
- # In order to test your success callback you should provide a :response object. Similarly, to test your failure
68
- # callback you should provide an :error object.
69
- #
70
- # Asserting the request object can be done one of two ways: direct or explicit. If you would like to directly test
71
- # the object that is given as a request you should provide a :request object as part of the cb_mocks third parameter hash.
72
- # Alternatively you can do an explicit assertion by providing a block to mock_remote_service. The block will be yielded with
73
- # the request object as its only parameter. This allows you to perform your own assertions on the request object
74
- # (e.g. only check a few of the fields in the request). Also note that if a :request param is given in the third param,
75
- # the block will be ignored.
76
- #
77
- # @example Testing the client on_success callback
78
- # # Method under test
79
- # def create_user(request)
80
- # status = 'unknown'
81
- # Proto::UserService.client.create(request) do |c|
82
- # c.on_success do |response|
83
- # status = response.status
84
- # end
85
- # end
86
- # status
87
- # end
88
- # ...
89
- #
90
- # # spec
91
- # it 'verifies the on_success method behaves correctly' do
92
- # mock_remote_service(Proto::UserService, :client, response: mock('response_mock', status: 'success'))
93
- # create_user(request).should eq('success')
94
- # end
95
- #
96
- # @example Testing the client on_failure callback
97
- # # Method under test
98
- # def create_user(request)
99
- # status = nil
100
- # Proto::UserService.client.create(request) do |c|
101
- # c.on_failure do |error|
102
- # status = 'error'
103
- # ErrorReporter.report(error.message)
104
- # end
105
- # end
106
- # status
107
- # end
108
- # ...
109
- #
110
- # # spec
111
- # it 'verifies the on_success method behaves correctly' do
112
- # mock_remote_service(Proto::UserService, :client, error: mock('error_mock', message: 'this is an error message'))
113
- # ErrorReporter.should_receive(:report).with('this is an error message')
114
- # create_user(request).should eq('error')
115
- # end
116
- #
117
- # @example Testing the given client request object (direct assert)
118
- # # Method under test
119
- # def create_user
120
- # request = ... # some operation to build a request on state
121
- # Proto::UserService.client.create(request) do |c|
122
- # ...
123
- # end
124
- # end
125
- # ...
126
- #
127
- # # spec
128
- # it 'verifies the request is built correctly' do
129
- # expected_request = ... # some expectation
130
- # mock_remote_service(Proto::UserService, :client, request: expected_request)
131
- # create_user(request)
132
- # end
133
- #
134
- # @example Testing the given client request object (explicit assert)
135
- # # Method under test
136
- # def create_user
137
- # request = ... # some operation to build a request on state
138
- # Proto::UserService.client.create(request) do |c|
139
- # ...
140
- # end
141
- # end
142
- # ...
143
- #
144
- # # spec
145
- # it 'verifies the request is built correctly' do
146
- # mock_remote_service(Proto::UserService, :client) do |given_request|
147
- # given_request.field1.should eq 'rainbows'
148
- # given_request.field2.should eq 'ponies'
149
- # end
150
- # create_user(request)
151
- # end
152
- #
153
- # @param [Class] klass the service class constant
154
- # @param [Symbol, String] method a symbol or string denoting the method to call
155
- # @param [Hash] cb_mocks provides expectation objects to invoke on_success (with :response), on_failure (with :error), and the request object (:request)
156
- # @param [Block] assert_block when given, will be invoked with the request message sent to the client method
157
- # @return [Mock] the stubbed out client mock
158
- def mock_remote_service(klass, method, cb_mocks={}, &assert_block)
159
- klass.stub(:client).and_return(client = mock('Client'))
160
- client.stub(method).and_yield(client)
161
- if cb_mocks[:request]
162
- client.should_receive(method).with(cb_mocks[:request])
163
- elsif block_given?
164
- client.should_receive(method) do |given_req|
165
- assert_block.call(given_req)
53
+
54
+ module InstanceMethods
55
+
56
+ def subject_service
57
+ self.class.subject_service
58
+ end
59
+
60
+ # Call a local service to test responses and behavior based on the given request.
61
+ # Should use to outside-in test a local RPC Service without testing the underlying socket implementation.
62
+ #
63
+ # @example Test a local service method
64
+ # # Implementation
65
+ # module Proto
66
+ # class UserService < Protobuf::Rpc::Service
67
+ # def create
68
+ # user = User.create_from_proto(request)
69
+ # if request.name
70
+ # respond_with(ProtoRepresenter.new(user))
71
+ # else
72
+ # rpc_failed 'Error: name required'
73
+ # end
74
+ # end
75
+ # end
76
+ # end
77
+ #
78
+ # # Spec
79
+ # describe Proto::UserService do
80
+ # describe '#create' do
81
+ # it 'creates a new user' do
82
+ # create_request = Proto::UserCreate.new(...)
83
+ # service = call_local_service(Proto::UserService, :create, create_request)
84
+ # service.response.should eq(some_response_object)
85
+ # end
86
+ #
87
+ # it 'fails when name is not given' do
88
+ # bad_req = { :name => nil }
89
+ # service = call_local_service(Proto::UserService, :create, :create_request) do |service|
90
+ # # Block is yielded before the method is invoked.
91
+ # service.should_receive(:rpc_failed).with('Error: name required')
92
+ # end
93
+ # end
94
+ # end
95
+ # end
96
+ #
97
+ # @param [Symbol, String] method a symbol or string denoting the method to call.
98
+ # @param [Protobuf::Message or Hash] request the request message of the expected type for the given method.
99
+ # @param [String] a string message indicating an rpc_failed expectation.
100
+ # @param [block] optionally provide a block which will be yielded the service instance just prior to invoking the rpc method.
101
+ # @return [Protobuf::Service] the service instance post-calling the rpc method.
102
+ def local_rpc(rpc_method, request, expected_error = nil)
103
+ request = subject_service.rpcs[rpc_method].request_type.new(request) if request.is_a?(Hash)
104
+ service = subject_service.new(rpc_method, request.serialize_to_string)
105
+
106
+ if block_given?
107
+ $stderr.puts '[Warning] Ignoring error expectation %s due to given block' % expected_error unless expected_error.blank?
108
+ yield(service)
109
+ else
110
+ if expected_error.blank?
111
+ service.should_not_receive(:rpc_failed)
112
+ else
113
+ service.should_receive(:rpc_failed).with(expected_error)
114
+ end
166
115
  end
167
- else
168
- client.should_receive(method)
116
+
117
+ service.__send__(rpc_method)
118
+ service
119
+ end
120
+
121
+ # Provides backwards compatability to bridge to the new local_rpc usage.
122
+ #
123
+ def call_local_service(klass, rpc_method, request, &block)
124
+ $stderr.puts '[Deprecated] call_local_service is deprecated. Please use local_rpc in conjunction with subject_service.'
125
+ self.class.service { klass }
126
+ local_rpc(rpc_method, request, nil &block)
169
127
  end
128
+ alias_method :call_service, :call_local_service
170
129
 
171
- if cb_mocks[:response]
172
- client.stub(:on_success).and_yield(cb_mocks[:response])
173
- else
174
- client.stub(:on_success)
130
+ # Create a mock service that responds in the way you are expecting to aid in testing client -> service calls.
131
+ # In order to test your success callback you should provide a :response object. Similarly, to test your failure
132
+ # callback you should provide an :error object.
133
+ #
134
+ # Asserting the request object can be done one of two ways: direct or explicit. If you would like to directly test
135
+ # the object that is given as a request you should provide a :request object as part of the cb_mocks third parameter hash.
136
+ # Alternatively you can do an explicit assertion by providing a block to mock_remote_service. The block will be yielded with
137
+ # the request object as its only parameter. This allows you to perform your own assertions on the request object
138
+ # (e.g. only check a few of the fields in the request). Also note that if a :request param is given in the third param,
139
+ # the block will be ignored.
140
+ #
141
+ # @example Testing the client on_success callback
142
+ # # Method under test
143
+ # def create_user(request)
144
+ # status = 'unknown'
145
+ # Proto::UserService.client.create(request) do |c|
146
+ # c.on_success do |response|
147
+ # status = response.status
148
+ # end
149
+ # end
150
+ # status
151
+ # end
152
+ # ...
153
+ #
154
+ # # spec
155
+ # it 'verifies the on_success method behaves correctly' do
156
+ # mock_remote_service(Proto::UserService, :client, response: mock('response_mock', status: 'success'))
157
+ # create_user(request).should eq('success')
158
+ # end
159
+ #
160
+ # @example Testing the client on_failure callback
161
+ # # Method under test
162
+ # def create_user(request)
163
+ # status = nil
164
+ # Proto::UserService.client.create(request) do |c|
165
+ # c.on_failure do |error|
166
+ # status = 'error'
167
+ # ErrorReporter.report(error.message)
168
+ # end
169
+ # end
170
+ # status
171
+ # end
172
+ # ...
173
+ #
174
+ # # spec
175
+ # it 'verifies the on_success method behaves correctly' do
176
+ # mock_remote_service(Proto::UserService, :client, error: mock('error_mock', message: 'this is an error message'))
177
+ # ErrorReporter.should_receive(:report).with('this is an error message')
178
+ # create_user(request).should eq('error')
179
+ # end
180
+ #
181
+ # @example Testing the given client request object (direct assert)
182
+ # # Method under test
183
+ # def create_user
184
+ # request = ... # some operation to build a request on state
185
+ # Proto::UserService.client.create(request) do |c|
186
+ # ...
187
+ # end
188
+ # end
189
+ # ...
190
+ #
191
+ # # spec
192
+ # it 'verifies the request is built correctly' do
193
+ # expected_request = ... # some expectation
194
+ # mock_remote_service(Proto::UserService, :client, request: expected_request)
195
+ # create_user(request)
196
+ # end
197
+ #
198
+ # @example Testing the given client request object (explicit assert)
199
+ # # Method under test
200
+ # def create_user
201
+ # request = ... # some operation to build a request on state
202
+ # Proto::UserService.client.create(request) do |c|
203
+ # ...
204
+ # end
205
+ # end
206
+ # ...
207
+ #
208
+ # # spec
209
+ # it 'verifies the request is built correctly' do
210
+ # mock_remote_service(Proto::UserService, :client) do |given_request|
211
+ # given_request.field1.should eq 'rainbows'
212
+ # given_request.field2.should eq 'ponies'
213
+ # end
214
+ # create_user(request)
215
+ # end
216
+ #
217
+ # @param [Class] klass the service class constant
218
+ # @param [Symbol, String] method a symbol or string denoting the method to call
219
+ # @param [Hash] cb_mocks provides expectation objects to invoke on_success (with :response), on_failure (with :error), and the request object (:request)
220
+ # @param [Block] assert_block when given, will be invoked with the request message sent to the client method
221
+ # @return [Mock] the stubbed out client mock
222
+ def mock_remote_service(klass, method, cb_mocks={}, &assert_block)
223
+ self.class.subject_service { klass }
224
+ mock_rpc(method, callbacks, &assert_block)
175
225
  end
226
+ alias_method :mock_service, :mock_remote_service
227
+
228
+ def mock_rpc(method, callbacks = {}, &assert_block)
229
+ client = double('Client', :on_success => true, :on_failure => true)
230
+ client.stub(method).and_yield(client)
231
+
232
+ subject_service.stub(:client).and_return(client)
233
+
234
+ if cb_mocks[:request]
235
+ client.should_receive(method).with(cb_mocks[:request])
236
+ elsif block_given?
237
+ client.should_receive(method) do |given_req|
238
+ assert_block.call(given_req)
239
+ end
240
+ else
241
+ client.should_receive(method)
242
+ end
243
+
244
+ client.stub(:on_success).and_yield(cb_mocks[:response]) if cb_mocks[:response]
245
+ client.stub(:on_failure).and_yield(cb_mocks[:error]) if cb_mocks[:error]
176
246
 
177
- if cb_mocks[:error]
178
- client.stub(:on_failure).and_yield(cb_mocks[:error])
179
- else
180
- client.stub(:on_failure)
247
+ client
181
248
  end
182
249
 
183
- client
184
250
  end
185
- alias_method :mock_service, :mock_remote_service
186
251
 
187
252
  end
188
253
  end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module RSpec
3
- VERSION = "0.1.0.rc2"
3
+ VERSION = "0.1.0.rc3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.rc2
4
+ version: 0.1.0.rc3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000Z
12
+ date: 2012-10-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: protobuf
16
- requirement: &2155337520 !ruby/object:Gem::Requirement
16
+ requirement: &2153681820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2155337520
24
+ version_requirements: *2153681820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2155337020 !ruby/object:Gem::Requirement
27
+ requirement: &2153681320 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.8'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2155337020
35
+ version_requirements: *2153681320
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &2155336640 !ruby/object:Gem::Requirement
38
+ requirement: &2153680800 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2155336640
46
+ version_requirements: *2153680800
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &2155336100 !ruby/object:Gem::Requirement
49
+ requirement: &2153680000 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0.7'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2155336100
57
+ version_requirements: *2153680000
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: redcarpet
60
- requirement: &2155335600 !ruby/object:Gem::Requirement
60
+ requirement: &2153679240 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '2.1'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2155335600
68
+ version_requirements: *2153679240
69
69
  description: Protobuf RSpec helpers for testing services and clients. Meant to be
70
70
  used with the protobuf gem. Decouple external services/clients from each other using
71
71
  the given helper methods.
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  segments:
99
99
  - 0
100
- hash: -4305518628667414038
100
+ hash: -133787478881464858
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  none: false
103
103
  requirements: