evil-client 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,168 @@
1
+ RSpec.describe Evil::Client::RSpec, "#stub_client_operation" do
2
+ include described_class
3
+ before { load "spec/fixtures/test_client.rb" }
4
+
5
+ let(:client) { Test::Client.new(subdomain: "x", user: "y", token: "z") }
6
+ let(:perform) { client.crm(version: 7).users.fetch id: 5 }
7
+
8
+ context "with client class" do
9
+ let(:klass) { Test::Client }
10
+
11
+ it "stubs the call with nil" do
12
+ stub_client_operation(klass).to_return nil
13
+
14
+ expect(perform).to be_nil
15
+ end
16
+
17
+ it "stubs the call with any object" do
18
+ result = double
19
+ stub_client_operation(klass).to_return result
20
+
21
+ expect(perform).to eq result
22
+ end
23
+
24
+ it "stubs the call with the original implementation" do
25
+ stub_client_operation(klass).to_call_original
26
+
27
+ expect(perform).to eq [200, {}, []]
28
+ end
29
+
30
+ it "stubs the call with StandardError" do
31
+ stub_client_operation(klass).to_raise
32
+
33
+ expect { perform }.to raise_error StandardError
34
+ end
35
+
36
+ it "stubs the call with an exception of given type" do
37
+ stub_client_operation(klass).to_raise(TypeError)
38
+
39
+ expect { perform }.to raise_error TypeError
40
+ end
41
+
42
+ it "stubs the call with given exception" do
43
+ stub_client_operation(klass).to_raise(TypeError, "foobar")
44
+
45
+ expect { perform }.to raise_error TypeError, /foobar/
46
+ end
47
+ end
48
+
49
+ context "with client's superclass" do
50
+ let(:klass) { Evil::Client }
51
+
52
+ it "stubs the call" do
53
+ stub_client_operation(klass).to_return
54
+ expect(perform).to be_nil
55
+ end
56
+ end
57
+
58
+ context "without params" do
59
+ it "stubs the call" do
60
+ stub_client_operation.to_return
61
+ expect(perform).to be_nil
62
+ end
63
+ end
64
+
65
+ context "with neither a client nor its superclass" do
66
+ let(:klass) { String }
67
+
68
+ it "don't stubs the call" do
69
+ stub_client_operation(klass).to_return
70
+ expect { perform }.to raise_error RSpec::Mocks::MockExpectationError
71
+ end
72
+ end
73
+
74
+ context "with client class and fully qualified name" do
75
+ let(:klass) { Test::Client }
76
+ let(:name) { "crm.users.fetch" }
77
+
78
+ it "stubs the call" do
79
+ stub_client_operation(klass, name).to_return
80
+ expect(perform).to be_nil
81
+ end
82
+ end
83
+
84
+ context "with client class and underqualified name" do
85
+ let(:klass) { Test::Client }
86
+ let(:name) { "crm.users" }
87
+
88
+ it "don't stubs the call" do
89
+ stub_client_operation(klass, name).to_return
90
+ expect { perform }.to raise_error RSpec::Mocks::MockExpectationError
91
+ end
92
+ end
93
+
94
+ context "with client class and partially qualified name" do
95
+ let(:klass) { Test::Client }
96
+ let(:name) { /crm\.users/ }
97
+
98
+ it "stubs the call" do
99
+ stub_client_operation(klass, name).to_return
100
+ expect(perform).to be_nil
101
+ end
102
+ end
103
+
104
+ context "with client class and wrongly qualified name" do
105
+ let(:klass) { Test::Client }
106
+ let(:name) { /^crm.users$/ }
107
+
108
+ it "don't stubs the call" do
109
+ stub_client_operation(klass, name).to_return
110
+ expect { perform }.to raise_error RSpec::Mocks::MockExpectationError
111
+ end
112
+ end
113
+
114
+ context "with client class and expected options" do
115
+ let(:klass) { Test::Client }
116
+ let(:name) { "crm.users.fetch" }
117
+ let(:opts) do
118
+ { subdomain: "x", user: "y", token: "z", version: 7, id: 5 }
119
+ end
120
+
121
+ it "stubs the call" do
122
+ stub_client_operation(klass, name).with(opts).to_return
123
+ expect(perform).to be_nil
124
+ end
125
+ end
126
+
127
+ context "with client class and block expectation" do
128
+ let(:klass) { Test::Client }
129
+ let(:name) { "crm.users.fetch" }
130
+ let(:opts) do
131
+ { subdomain: "x", user: "y", token: "z", version: 7, id: 5 }
132
+ end
133
+
134
+ it "stubs the call" do
135
+ stub_client_operation(klass, name).with { |o| o == opts }.to_return
136
+ expect(perform).to be_nil
137
+ end
138
+
139
+ it "stubs the call to any returned value" do
140
+ stub_client_operation(klass, name).with { |_o| true }.to_return 42
141
+ expect(perform).to eq 42
142
+ end
143
+ end
144
+
145
+ context "with client class and overexpected options" do
146
+ let(:klass) { Test::Client }
147
+ let(:name) { "crm.users.fetch" }
148
+ let(:opts) do
149
+ { subdomain: "x", user: "y", token: "z", version: 7 }
150
+ end
151
+
152
+ it "don't stubs the call" do
153
+ stub_client_operation(klass, name).with { |o| o == opts }.to_return
154
+ expect { perform }.to raise_error RSpec::Mocks::MockExpectationError
155
+ end
156
+ end
157
+
158
+ context "with client class and partially expected options" do
159
+ let(:klass) { Test::Client }
160
+ let(:name) { "crm.users.fetch" }
161
+ let(:opts) { hash_including subdomain: "x" }
162
+
163
+ it "stubs the call" do
164
+ stub_client_operation(klass, name).with(opts).to_return
165
+ expect(perform).to be_nil
166
+ end
167
+ end
168
+ end
@@ -175,6 +175,11 @@ RSpec.describe Evil::Client::Settings do
175
175
  it "slices declared options from the assigned ones" do
176
176
  expect(subject).to eq id: 42
177
177
  end
178
+
179
+ it "responds to #slice and #except" do
180
+ expect(subject).to respond_to :slice
181
+ expect(subject).to respond_to :except
182
+ end
178
183
  end
179
184
 
180
185
  describe "#datetime" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evil-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin (nepalez)
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-08-06 00:00:00.000000000 Z
12
+ date: 2017-08-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-initializer
@@ -59,14 +59,14 @@ dependencies:
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '1'
62
+ version: '2'
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '1'
69
+ version: '2'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rake
72
72
  requirement: !ruby/object:Gem::Requirement
@@ -210,6 +210,8 @@ files:
210
210
  - lib/evil/client/formatter/multipart.rb
211
211
  - lib/evil/client/formatter/part.rb
212
212
  - lib/evil/client/formatter/text.rb
213
+ - lib/evil/client/names.rb
214
+ - lib/evil/client/options.rb
213
215
  - lib/evil/client/resolver.rb
214
216
  - lib/evil/client/resolver/body.rb
215
217
  - lib/evil/client/resolver/format.rb
@@ -222,6 +224,10 @@ files:
222
224
  - lib/evil/client/resolver/security.rb
223
225
  - lib/evil/client/resolver/uri.rb
224
226
  - lib/evil/client/rspec.rb
227
+ - lib/evil/client/rspec/allow_stub.rb
228
+ - lib/evil/client/rspec/base_stub.rb
229
+ - lib/evil/client/rspec/evil_client_schema_matching.rb
230
+ - lib/evil/client/rspec/expect_stub.rb
225
231
  - lib/evil/client/schema.rb
226
232
  - lib/evil/client/schema/operation.rb
227
233
  - lib/evil/client/schema/scope.rb
@@ -255,6 +261,7 @@ files:
255
261
  - spec/unit/formatter/part_spec.rb
256
262
  - spec/unit/formatter/text_spec.rb
257
263
  - spec/unit/formatter_spec.rb
264
+ - spec/unit/options_spec.rb
258
265
  - spec/unit/resolver/body_spec.rb
259
266
  - spec/unit/resolver/format_spec.rb
260
267
  - spec/unit/resolver/headers_spec.rb
@@ -265,7 +272,9 @@ files:
265
272
  - spec/unit/resolver/response_spec.rb
266
273
  - spec/unit/resolver/security_spec.rb
267
274
  - spec/unit/resolver/uri_spec.rb
268
- - spec/unit/rspec_spec.rb
275
+ - spec/unit/rspec/evil_client_shema_matching_spec.rb
276
+ - spec/unit/rspec/expect_client_operation_spec.rb
277
+ - spec/unit/rspec/stub_client_operation_spec.rb
269
278
  - spec/unit/schema/operation_spec.rb
270
279
  - spec/unit/schema/scope_spec.rb
271
280
  - spec/unit/schema_spec.rb
@@ -323,6 +332,7 @@ test_files:
323
332
  - spec/unit/formatter/part_spec.rb
324
333
  - spec/unit/formatter/text_spec.rb
325
334
  - spec/unit/formatter_spec.rb
335
+ - spec/unit/options_spec.rb
326
336
  - spec/unit/resolver/body_spec.rb
327
337
  - spec/unit/resolver/format_spec.rb
328
338
  - spec/unit/resolver/headers_spec.rb
@@ -333,7 +343,9 @@ test_files:
333
343
  - spec/unit/resolver/response_spec.rb
334
344
  - spec/unit/resolver/security_spec.rb
335
345
  - spec/unit/resolver/uri_spec.rb
336
- - spec/unit/rspec_spec.rb
346
+ - spec/unit/rspec/evil_client_shema_matching_spec.rb
347
+ - spec/unit/rspec/expect_client_operation_spec.rb
348
+ - spec/unit/rspec/stub_client_operation_spec.rb
337
349
  - spec/unit/schema/operation_spec.rb
338
350
  - spec/unit/schema/scope_spec.rb
339
351
  - spec/unit/schema_spec.rb
@@ -1,342 +0,0 @@
1
- RSpec.describe "rspec matcher :perform_operation" do
2
- before { load "spec/fixtures/test_client.rb" }
3
-
4
- let(:client) { Test::Client.new(subdomain: "foo", user: "bar", token: "baz") }
5
- let(:users) { client.crm(version: 7).users }
6
-
7
- let(:fetch) { users.fetch id: 5 }
8
- let(:update_7) { users.update id: 5, name: "Joe", language: "en" }
9
- let(:update_8) { users.update id: 5, name: "Joe", language: "en", version: 8 }
10
-
11
- let(:name) { "Test::Client.crm.users.update" }
12
- let(:block) { proc { |version:, **| version == 7 } }
13
-
14
- describe "base matcher" do
15
- context "in a positive check" do
16
- subject { expect { code }.to perform_operation(name) }
17
-
18
- context "when operaton with expected name was performed" do
19
- let(:code) { fetch; update_7 }
20
-
21
- it "passes" do
22
- expect { subject }.not_to raise_error
23
- end
24
- end
25
-
26
- context "when operation with expected name wasn't performed" do
27
- let(:code) { fetch }
28
-
29
- it "fails" do
30
- expect { subject }
31
- .to raise_error RSpec::Expectations::ExpectationNotMetError
32
- end
33
- end
34
- end
35
-
36
- context "in a negative check" do
37
- subject { expect { code }.not_to perform_operation(name) }
38
-
39
- context "when operaton with expected name was performed" do
40
- let(:code) { fetch; update_7 }
41
-
42
- it "fails" do
43
- expect { subject }
44
- .to raise_error RSpec::Expectations::ExpectationNotMetError
45
- end
46
- end
47
-
48
- context "when operation with expected name wasn't performed" do
49
- let(:code) { fetch }
50
-
51
- it "passes" do
52
- expect { subject }.not_to raise_error
53
- end
54
- end
55
- end
56
- end
57
-
58
- describe "block argument" do
59
- context "in a positive check" do
60
- subject { expect { code }.to perform_operation(name, &block) }
61
-
62
- context "when operaton with expected name/options was performed" do
63
- let(:code) { fetch; update_7 }
64
-
65
- it "passes" do
66
- expect { subject }.not_to raise_error
67
- end
68
- end
69
-
70
- context "when options differ from expected" do
71
- let(:code) { fetch; update_8 }
72
-
73
- it "fails" do
74
- expect { subject }
75
- .to raise_error RSpec::Expectations::ExpectationNotMetError
76
- end
77
- end
78
-
79
- context "when operation wasn't performed" do
80
- let(:code) { fetch }
81
-
82
- it "fails" do
83
- expect { subject }
84
- .to raise_error RSpec::Expectations::ExpectationNotMetError
85
- end
86
- end
87
- end
88
-
89
- context "in a negagive check" do
90
- subject { expect { code }.not_to perform_operation(name, &block) }
91
-
92
- context "when operaton with expected name/options was performed" do
93
- let(:code) { fetch; update_7 }
94
-
95
- it "fails" do
96
- expect { subject }
97
- .to raise_error RSpec::Expectations::ExpectationNotMetError
98
- end
99
- end
100
-
101
- context "when options differ from expected" do
102
- let(:code) { fetch; update_8 }
103
-
104
- it "passes" do
105
- expect { subject }.not_to raise_error
106
- end
107
- end
108
-
109
- context "when operation wasn't performed" do
110
- let(:code) { fetch }
111
-
112
- it "passes" do
113
- expect { subject }.not_to raise_error
114
- end
115
- end
116
- end
117
- end
118
-
119
- describe "#with modifier" do
120
- let(:opts) { { version: 7 } }
121
-
122
- context "in a positive check" do
123
- subject { expect { code }.to perform_operation(name).with(opts) }
124
-
125
- context "when operaton with expected name/options was performed" do
126
- let(:code) { fetch; update_7 }
127
-
128
- it "passes" do
129
- expect { subject }.not_to raise_error
130
- end
131
- end
132
-
133
- context "when options differ from expected" do
134
- let(:code) { fetch; update_8 }
135
-
136
- it "fails" do
137
- expect { subject }
138
- .to raise_error RSpec::Expectations::ExpectationNotMetError
139
- end
140
- end
141
-
142
- context "when operation wasn't performed" do
143
- let(:code) { fetch }
144
-
145
- it "fails" do
146
- expect { subject }
147
- .to raise_error RSpec::Expectations::ExpectationNotMetError
148
- end
149
- end
150
- end
151
-
152
- context "in a negative check" do
153
- subject { expect { code }.not_to perform_operation(name).with(opts) }
154
-
155
- context "when operaton with expected name/options was performed" do
156
- let(:code) { fetch; update_7 }
157
-
158
- it "fails" do
159
- expect { subject }
160
- .to raise_error RSpec::Expectations::ExpectationNotMetError
161
- end
162
- end
163
-
164
- context "when options differ from expected" do
165
- let(:code) { fetch; update_8 }
166
-
167
- it "passes" do
168
- expect { subject }.not_to raise_error
169
- end
170
- end
171
-
172
- context "when operation wasn't performed" do
173
- let(:code) { fetch }
174
-
175
- it "passes" do
176
- expect { subject }.not_to raise_error
177
- end
178
- end
179
- end
180
- end
181
-
182
- describe "#with_exactly modifier" do
183
- let(:opts) do
184
- {
185
- id: 5,
186
- language: "en",
187
- name: "Joe",
188
- subdomain: "foo",
189
- token: "baz",
190
- user: "bar",
191
- version: 7
192
- }
193
- end
194
-
195
- context "in a positive check" do
196
- subject { expect { code }.to perform_operation(name).with_exactly(opts) }
197
-
198
- context "when operaton with expected name/options was performed" do
199
- let(:code) { fetch; update_7 }
200
-
201
- it "passes" do
202
- expect { subject }.not_to raise_error
203
- end
204
- end
205
-
206
- context "when some options differ from expected" do
207
- let(:code) { fetch; update_8 }
208
-
209
- it "fails" do
210
- expect { subject }
211
- .to raise_error RSpec::Expectations::ExpectationNotMetError
212
- end
213
- end
214
-
215
- context "when some options are not expected" do
216
- before { opts.delete :version }
217
- let(:code) { fetch; update_7 }
218
-
219
- it "fails" do
220
- expect { subject }
221
- .to raise_error RSpec::Expectations::ExpectationNotMetError
222
- end
223
- end
224
-
225
- context "when operation wasn't performed" do
226
- let(:code) { fetch }
227
-
228
- it "fails" do
229
- expect { subject }
230
- .to raise_error RSpec::Expectations::ExpectationNotMetError
231
- end
232
- end
233
- end
234
-
235
- context "in a negative check" do
236
- subject do
237
- expect { code }.not_to perform_operation(name).with_exactly(opts)
238
- end
239
-
240
- context "when operaton with expected name/options was performed" do
241
- let(:code) { fetch; update_7 }
242
-
243
- it "fails" do
244
- expect { subject }
245
- .to raise_error RSpec::Expectations::ExpectationNotMetError
246
- end
247
- end
248
-
249
- context "when some options differ from expected" do
250
- let(:code) { fetch; update_8 }
251
-
252
- it "passes" do
253
- expect { subject }.not_to raise_error
254
- end
255
- end
256
-
257
- context "when some options are not expected" do
258
- before { opts.delete :version }
259
- let(:code) { fetch; update_7 }
260
-
261
- it "passes" do
262
- expect { subject }.not_to raise_error
263
- end
264
- end
265
-
266
- context "when operation wasn't performed" do
267
- let(:code) { fetch }
268
-
269
- it "passes" do
270
- expect { subject }.not_to raise_error
271
- end
272
- end
273
- end
274
- end
275
-
276
- describe "#without modifier" do
277
- context "in a positive check" do
278
- subject { expect { code }.to perform_operation(name).without(opts) }
279
-
280
- context "when operaton without options was performed" do
281
- let(:opts) { %i[phone] }
282
- let(:code) { fetch; update_7 }
283
-
284
- it "passes" do
285
- expect { subject }.not_to raise_error
286
- end
287
- end
288
-
289
- context "when operation contains a forbidden option" do
290
- let(:opts) { %i[id] }
291
- let(:code) { fetch; update_7 }
292
-
293
- it "fails" do
294
- expect { subject }
295
- .to raise_error RSpec::Expectations::ExpectationNotMetError
296
- end
297
- end
298
-
299
- context "when operation wasn't performed" do
300
- let(:opts) { %i[phone] }
301
- let(:code) { fetch }
302
-
303
- it "fails" do
304
- expect { subject }
305
- .to raise_error RSpec::Expectations::ExpectationNotMetError
306
- end
307
- end
308
- end
309
-
310
- context "in a negative check" do
311
- subject { expect { code }.not_to perform_operation(name).without(opts) }
312
-
313
- context "when operaton without options was performed" do
314
- let(:opts) { %i[phone] }
315
- let(:code) { fetch; update_7 }
316
-
317
- it "fails" do
318
- expect { subject }
319
- .to raise_error RSpec::Expectations::ExpectationNotMetError
320
- end
321
- end
322
-
323
- context "when operation contains a forbidden option" do
324
- let(:opts) { %i[id] }
325
- let(:code) { fetch; update_7 }
326
-
327
- it "passes" do
328
- expect { subject }.not_to raise_error
329
- end
330
- end
331
-
332
- context "when operation wasn't performed" do
333
- let(:opts) { %i[phone] }
334
- let(:code) { fetch }
335
-
336
- it "passes" do
337
- expect { subject }.not_to raise_error
338
- end
339
- end
340
- end
341
- end
342
- end