protobuf-rspec 0.0.4 → 0.1.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/protobuf/rspec/helpers.rb +40 -78
- data/lib/protobuf/rspec/version.rb +1 -1
- data/protobuf-rspec.gemspec +2 -1
- metadata +27 -13
@@ -12,86 +12,63 @@ module Protobuf
|
|
12
12
|
module RSpec
|
13
13
|
module Helpers
|
14
14
|
|
15
|
-
ClientMock = Struct.new("ClientMock", :request, :response, :error)
|
16
|
-
|
17
15
|
# Call a local service to test responses and behavior based on the given request.
|
18
16
|
# Should use to outside-in test a local RPC Service without testing the underlying socket implementation.
|
19
|
-
#
|
17
|
+
#
|
20
18
|
# @example Test a local service method
|
21
19
|
# # Implementation
|
22
20
|
# module Proto
|
23
21
|
# class UserService < Protobuf::Rpc::Service
|
24
22
|
# def create
|
25
23
|
# user = User.create_from_proto(request)
|
26
|
-
#
|
24
|
+
# if request.name
|
25
|
+
# respond_with(ProtoRepresenter.new(user))
|
26
|
+
# else
|
27
|
+
# rpc_failed 'Error: name required'
|
28
|
+
# end
|
27
29
|
# end
|
28
30
|
# end
|
29
31
|
# end
|
30
|
-
#
|
32
|
+
#
|
31
33
|
# # Spec
|
32
34
|
# describe Proto::UserService do
|
33
35
|
# describe '#create' do
|
34
36
|
# it 'creates a new user' do
|
35
37
|
# create_request = Proto::UserCreate.new(...)
|
36
|
-
#
|
37
|
-
#
|
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
|
38
48
|
# end
|
39
49
|
# end
|
40
50
|
# end
|
41
51
|
#
|
42
|
-
# @param [Class] klass the service class constant
|
43
|
-
# @param [Symbol, String] method a symbol or string denoting the method to call
|
44
|
-
# @param [Protobuf::Message] request the request message of the expected type for the given method
|
45
|
-
# @param [
|
46
|
-
# @return [
|
47
|
-
def call_local_service(klass,
|
48
|
-
|
49
|
-
|
50
|
-
service
|
51
|
-
service.
|
52
|
-
|
53
|
-
@response = res
|
54
|
-
self.stub(:response).and_return(@response)
|
55
|
-
end
|
56
|
-
def service.rpc_failed message
|
57
|
-
@custom_error = message
|
58
|
-
send_response
|
59
|
-
end
|
60
|
-
def service.send_response
|
61
|
-
@send_response_called = true
|
62
|
-
end
|
63
|
-
|
64
|
-
client_mock = ClientMock.new
|
65
|
-
client_mock.request = request
|
66
|
-
|
67
|
-
begin
|
68
|
-
yield(service) if block_given?
|
69
|
-
service.__send__("rpc_#{method}")
|
70
|
-
rescue
|
71
|
-
client_mock.error = $!
|
72
|
-
else
|
73
|
-
client_mock.error = service.instance_variable_get(:@custom_error)
|
74
|
-
ensure
|
75
|
-
if client_mock.error.nil?
|
76
|
-
if service.instance_variable_get(:@async_responder)
|
77
|
-
Timeout.timeout(timeout) do
|
78
|
-
sleep 0.5 until service.instance_variable_get(:@send_response_called)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
client_mock.response = service.instance_variable_get(:@response) || response
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
client_mock
|
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 = service.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
|
86
63
|
end
|
87
|
-
|
64
|
+
alias_method :call_service, :call_local_service
|
88
65
|
|
89
66
|
# Create a mock service that responds in the way you are expecting to aid in testing client -> service calls.
|
90
67
|
# 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.
|
92
|
-
#
|
68
|
+
# callback you should provide an :error object.
|
69
|
+
#
|
93
70
|
# 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.
|
71
|
+
# the object that is given as a request you should provide a :request object as part of the cb_mocks third parameter hash.
|
95
72
|
# Alternatively you can do an explicit assertion by providing a block to mock_remote_service. The block will be yielded with
|
96
73
|
# the request object as its only parameter. This allows you to perform your own assertions on the request object
|
97
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,
|
@@ -109,7 +86,7 @@ module Protobuf
|
|
109
86
|
# status
|
110
87
|
# end
|
111
88
|
# ...
|
112
|
-
#
|
89
|
+
#
|
113
90
|
# # spec
|
114
91
|
# it 'verifies the on_success method behaves correctly' do
|
115
92
|
# mock_remote_service(Proto::UserService, :client, response: mock('response_mock', status: 'success'))
|
@@ -129,7 +106,7 @@ module Protobuf
|
|
129
106
|
# status
|
130
107
|
# end
|
131
108
|
# ...
|
132
|
-
#
|
109
|
+
#
|
133
110
|
# # spec
|
134
111
|
# it 'verifies the on_success method behaves correctly' do
|
135
112
|
# mock_remote_service(Proto::UserService, :client, error: mock('error_mock', message: 'this is an error message'))
|
@@ -146,14 +123,14 @@ module Protobuf
|
|
146
123
|
# end
|
147
124
|
# end
|
148
125
|
# ...
|
149
|
-
#
|
126
|
+
#
|
150
127
|
# # spec
|
151
128
|
# it 'verifies the request is built correctly' do
|
152
129
|
# expected_request = ... # some expectation
|
153
130
|
# mock_remote_service(Proto::UserService, :client, request: expected_request)
|
154
131
|
# create_user(request)
|
155
132
|
# end
|
156
|
-
#
|
133
|
+
#
|
157
134
|
# @example Testing the given client request object (explicit assert)
|
158
135
|
# # Method under test
|
159
136
|
# def create_user
|
@@ -163,7 +140,7 @@ module Protobuf
|
|
163
140
|
# end
|
164
141
|
# end
|
165
142
|
# ...
|
166
|
-
#
|
143
|
+
#
|
167
144
|
# # spec
|
168
145
|
# it 'verifies the request is built correctly' do
|
169
146
|
# mock_remote_service(Proto::UserService, :client) do |given_request|
|
@@ -172,7 +149,7 @@ module Protobuf
|
|
172
149
|
# end
|
173
150
|
# create_user(request)
|
174
151
|
# end
|
175
|
-
#
|
152
|
+
#
|
176
153
|
# @param [Class] klass the service class constant
|
177
154
|
# @param [Symbol, String] method a symbol or string denoting the method to call
|
178
155
|
# @param [Hash] cb_mocks provides expectation objects to invoke on_success (with :response), on_failure (with :error), and the request object (:request)
|
@@ -202,25 +179,10 @@ module Protobuf
|
|
202
179
|
else
|
203
180
|
client.stub(:on_failure)
|
204
181
|
end
|
205
|
-
|
206
|
-
client
|
207
|
-
end
|
208
|
-
alias :mock_service :mock_remote_service
|
209
182
|
|
210
|
-
|
211
|
-
# It's debatable if this is actually helpful since the protobuf message is so lean in the first place.
|
212
|
-
#
|
213
|
-
# @param [Class, String] klass the message class constant or the message name
|
214
|
-
# @param [Hash] stubs the stubbed fields and values
|
215
|
-
# @return [OpenStruct] the mocked message
|
216
|
-
def mock_proto(klass, stubs={})
|
217
|
-
proto_instance = OpenStruct.new({:mock_name => klass}.merge(stubs))
|
218
|
-
proto_instance.stub!(:has_field? => true)
|
219
|
-
proto = mock(klass)
|
220
|
-
proto.stub!(:tap).and_yield(proto_instance)
|
221
|
-
klass.stub!(:new).and_return(proto)
|
222
|
-
proto_instance
|
183
|
+
client
|
223
184
|
end
|
185
|
+
alias_method :mock_service, :mock_remote_service
|
224
186
|
|
225
187
|
end
|
226
188
|
end
|
data/protobuf-rspec.gemspec
CHANGED
@@ -18,9 +18,10 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
# specify any dependencies here; for example:
|
22
21
|
s.add_runtime_dependency "protobuf", ">= 1.1"
|
23
22
|
s.add_runtime_dependency "rspec", "~> 2.8"
|
23
|
+
|
24
|
+
s.add_development_dependency "rake"
|
24
25
|
s.add_development_dependency "yard", "~> 0.7"
|
25
26
|
s.add_development_dependency "redcarpet", "~> 2.1"
|
26
27
|
end
|
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: protobuf-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0.rc1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- BJ Neilsen
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-16 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: protobuf
|
16
|
-
requirement: &
|
16
|
+
requirement: &2161458660 !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: *
|
24
|
+
version_requirements: *2161458660
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &2161457900 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,21 @@ dependencies:
|
|
32
32
|
version: '2.8'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2161457900
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &2161457260 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2161457260
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: yard
|
38
|
-
requirement: &
|
49
|
+
requirement: &2161456720 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ~>
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: '0.7'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *2161456720
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: redcarpet
|
49
|
-
requirement: &
|
60
|
+
requirement: &2161456140 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ~>
|
@@ -54,7 +65,7 @@ dependencies:
|
|
54
65
|
version: '2.1'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *2161456140
|
58
69
|
description: Protobuf RSpec helpers for testing services and clients. Meant to be
|
59
70
|
used with the protobuf gem. Decouple external services/clients from each other using
|
60
71
|
the given helper methods.
|
@@ -84,12 +95,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
95
|
- - ! '>='
|
85
96
|
- !ruby/object:Gem::Version
|
86
97
|
version: '0'
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
hash: -1776446749920294311
|
87
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
102
|
none: false
|
89
103
|
requirements:
|
90
|
-
- - ! '
|
104
|
+
- - ! '>'
|
91
105
|
- !ruby/object:Gem::Version
|
92
|
-
version:
|
106
|
+
version: 1.3.1
|
93
107
|
requirements: []
|
94
108
|
rubyforge_project: protobuf-rspec
|
95
109
|
rubygems_version: 1.8.15
|