protobuf-rspec 0.0.1 → 0.0.2

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.
data/README.md CHANGED
@@ -41,7 +41,9 @@ end
41
41
  Mocking Service Responses
42
42
  -------------------------
43
43
 
44
- Create a mock service that responds in the way you are expecting to aid in testing client -> service calls. In order to test your success callback you should provide a :response object. Similarly, to test your failure callback you should provide an :error object. If you would like to test the object that is given as a request you should provide a :request object.
44
+ Create a mock service that responds in the way you are expecting to aid in testing client -> service calls. In order to test your success callback you should provide a `:response` object. Similarly, to test your failure callback you should provide an `:error` object.
45
+
46
+ Asserting the request object can be done one of two ways: direct or explicit. If you would like to directly test the object that is given as a request you should provide a `:request` object as part of the `cb_mocks` hash (third parameter). Alternatively you can do an explicit assertion by providing a block to `mock_remote_service`. The block will be yielded with the request object as its only parameter. This allows you to perform your own assertions on the request object (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, the block will be ignored.
45
47
 
46
48
  ### Testing the client on_success callback
47
49
  ```ruby
@@ -87,7 +89,7 @@ it 'verifies the on_success method behaves correctly' do
87
89
  end
88
90
  ```
89
91
 
90
- ### Testing the given client request object
92
+ ### Testing the given client request object (direct assert)
91
93
  ```ruby
92
94
  # Method under test
93
95
  def create_user
@@ -106,7 +108,29 @@ it 'verifies the request is built correctly' do
106
108
  end
107
109
  ```
108
110
 
111
+ ### Testing the given client request object (explicit assert)
112
+ ```ruby
113
+ # Method under test
114
+ def create_user
115
+ request = ... # some operation to build a request on state
116
+ Proto::UserService.client.create(request) do |c|
117
+ ...
118
+ end
119
+ end
120
+ ...
121
+
122
+ # spec
123
+ it 'verifies the request is built correctly' do
124
+ mock_remote_service(Proto::UserService, :client) do |given_request|
125
+ given_request.field1.should eq 'rainbows'
126
+ given_request.field2.should eq 'ponies'
127
+ end
128
+ create_user(request)
129
+ end
130
+ ````
131
+
109
132
  Feedback
133
+ --------
110
134
 
111
135
  Feedback and comments are welcome:
112
136
 
@@ -88,8 +88,14 @@ module Protobuf
88
88
 
89
89
  # Create a mock service that responds in the way you are expecting to aid in testing client -> service calls.
90
90
  # In order to test your success callback you should provide a :response object. Similarly, to test your failure
91
- # callback you should provide an :error object. If you would like to test the object that is given as a request
92
- # you should provide a :request object.
91
+ # callback you should provide an :error object.
92
+ #
93
+ # Asserting the request object can be done one of two ways: direct or explicit. If you would like to directly test
94
+ # the object that is given as a request you should provide a :request object as part of the cb_mocks third parameter hash.
95
+ # Alternatively you can do an explicit assertion by providing a block to mock_remote_service. The block will be yielded with
96
+ # the request object as its only parameter. This allows you to perform your own assertions on the request object
97
+ # (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,
98
+ # the block will be ignored.
93
99
  #
94
100
  # @example Testing the client on_success callback
95
101
  # # Method under test
@@ -131,7 +137,7 @@ module Protobuf
131
137
  # create_user(request).should eq('error')
132
138
  # end
133
139
  #
134
- # @example Testing the given client request object
140
+ # @example Testing the given client request object (direct assert)
135
141
  # # Method under test
136
142
  # def create_user
137
143
  # request = ... # some operation to build a request on state
@@ -148,16 +154,40 @@ module Protobuf
148
154
  # create_user(request)
149
155
  # end
150
156
  #
157
+ # @example Testing the given client request object (explicit assert)
158
+ # # Method under test
159
+ # def create_user
160
+ # request = ... # some operation to build a request on state
161
+ # Proto::UserService.client.create(request) do |c|
162
+ # ...
163
+ # end
164
+ # end
165
+ # ...
166
+ #
167
+ # # spec
168
+ # it 'verifies the request is built correctly' do
169
+ # mock_remote_service(Proto::UserService, :client) do |given_request|
170
+ # given_request.field1.should eq 'rainbows'
171
+ # given_request.field2.should eq 'ponies'
172
+ # end
173
+ # create_user(request)
174
+ # end
175
+ #
151
176
  # @param [Class] klass the service class constant
152
177
  # @param [Symbol, String] method a symbol or string denoting the method to call
153
178
  # @param [Hash] cb_mocks provides expectation objects to invoke on_success (with :response), on_failure (with :error), and the request object (:request)
179
+ # @param [Block] assert_block when given, will be invoked with the request message sent to the client method
154
180
  # @return [Mock] the stubbed out client mock
155
- def mock_remote_service(klass, method, cb_mocks={})
181
+ def mock_remote_service(klass, method, cb_mocks={}, &assert_block)
156
182
  cb_mocks = {:error => mock('error', :message => nil, :code => nil)}.merge(cb_mocks)
157
183
  klass.stub(:client).and_return(client = mock('Client'))
158
184
  client.stub(method).and_yield(client)
159
185
  if cb_mocks[:request]
160
186
  client.should_receive(method).with(cb_mocks[:request])
187
+ elsif block_given?
188
+ client.should_receive(method) do |given_req|
189
+ assert_block.call(given_req)
190
+ end
161
191
  else
162
192
  client.should_receive(method)
163
193
  end
@@ -173,6 +203,7 @@ module Protobuf
173
203
  else
174
204
  client.stub(:on_failure)
175
205
  end
206
+
176
207
  client
177
208
  end
178
209
  alias :mock_service :mock_remote_service
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module RSpec
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
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.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: protobuf
16
- requirement: &2160846800 !ruby/object:Gem::Requirement
16
+ requirement: &2152859620 !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: *2160846800
24
+ version_requirements: *2152859620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2160845600 !ruby/object:Gem::Requirement
27
+ requirement: &2152858520 !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: *2160845600
35
+ version_requirements: *2152858520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: yard
38
- requirement: &2160844920 !ruby/object:Gem::Requirement
38
+ requirement: &2152857540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0.7'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2160844920
46
+ version_requirements: *2152857540
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: redcarpet
49
- requirement: &2160843920 !ruby/object:Gem::Requirement
49
+ requirement: &2152856760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '2.1'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2160843920
57
+ version_requirements: *2152856760
58
58
  description: Protobuf RSpec helpers for testing services and clients. Meant to be
59
59
  used with the protobuf gem. Decouple external services/clients from each other using
60
60
  the given helper methods.