spy 1.0.3 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +38 -0
- data/.tool-versions +1 -1
- data/CHANGELOG.md +20 -0
- data/README.md +3 -4
- data/lib/spy/call_log.rb +6 -3
- data/lib/spy/subroutine.rb +14 -26
- data/lib/spy/version.rb +1 -1
- data/spy.gemspec +1 -1
- data/test/spy/test_subroutine.rb +22 -0
- data/test/support/pen.rb +4 -0
- metadata +4 -36
- data/.travis.yml +0 -13
- data/spec/spec_helper.rb +0 -41
- data/spec/spy/and_call_original_spec.rb +0 -152
- data/spec/spy/and_yield_spec.rb +0 -123
- data/spec/spy/any_instance_spec.rb +0 -518
- data/spec/spy/hash_excluding_matcher_spec.rb +0 -63
- data/spec/spy/hash_including_matcher_spec.rb +0 -86
- data/spec/spy/mutate_const_spec.rb +0 -471
- data/spec/spy/nil_expectation_warning_spec.rb +0 -56
- data/spec/spy/null_object_mock_spec.rb +0 -79
- data/spec/spy/partial_mock_spec.rb +0 -81
- data/spec/spy/passing_argument_matchers_spec.rb +0 -140
- data/spec/spy/serialization_spec.rb +0 -116
- data/spec/spy/stash_spec.rb +0 -47
- data/spec/spy/stub_implementation_spec.rb +0 -64
- data/spec/spy/stub_spec.rb +0 -79
- data/spec/spy/to_ary_spec.rb +0 -40
@@ -1,518 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Spy
|
4
|
-
describe "#any_instance" do
|
5
|
-
class CustomErrorForAnyInstanceSpec < StandardError;end
|
6
|
-
|
7
|
-
let(:klass) do
|
8
|
-
Class.new do
|
9
|
-
def existing_method; :existing_method_return_value; end
|
10
|
-
def existing_method_with_arguments(arg_one, arg_two = nil); :existing_method_with_arguments_return_value; end
|
11
|
-
def another_existing_method; end
|
12
|
-
private
|
13
|
-
def private_method; :private_method_return_value; end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
let(:existing_method_return_value){ :existing_method_return_value }
|
17
|
-
|
18
|
-
context "with #stub" do
|
19
|
-
it "does not suppress an exception when a method that doesn't exist is invoked" do
|
20
|
-
Spy.on_instance_method(klass, :existing_method)
|
21
|
-
expect { klass.new.bar }.to raise_error(NoMethodError)
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'multiple methods' do
|
25
|
-
it "allows multiple methods to be stubbed in a single invocation" do
|
26
|
-
Spy.on_instance_method(klass, :existing_method => 'foo', :another_existing_method => 'bar')
|
27
|
-
instance = klass.new
|
28
|
-
expect(instance.existing_method).to eq('foo')
|
29
|
-
expect(instance.another_existing_method).to eq('bar')
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context "behaves as 'every instance'" do
|
34
|
-
it "stubs every instance in the spec" do
|
35
|
-
Subroutine.new(klass, :foo, false).hook(force: true).and_return(result = Object.new)
|
36
|
-
expect(klass.new.foo).to eq(result)
|
37
|
-
expect(klass.new.foo).to eq(result)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "stubs instance created before any_instance was called" do
|
41
|
-
instance = klass.new
|
42
|
-
Spy.on_instance_method(klass, :existing_method).and_return(result = Object.new)
|
43
|
-
expect(instance.existing_method).to eq(result)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context "with #and_return" do
|
48
|
-
it "stubs a method that doesn't exist" do
|
49
|
-
Spy.on_instance_method(klass, :existing_method).and_return(1)
|
50
|
-
expect(klass.new.existing_method).to eq(1)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "stubs a method that exists" do
|
54
|
-
Spy.on_instance_method(klass, :existing_method).and_return(1)
|
55
|
-
expect(klass.new.existing_method).to eq(1)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "returns the same object for calls on different instances" do
|
59
|
-
return_value = Object.new
|
60
|
-
Spy.on_instance_method(klass, :existing_method).and_return(return_value)
|
61
|
-
expect(klass.new.existing_method).to be(return_value)
|
62
|
-
expect(klass.new.existing_method).to be(return_value)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "with #and_yield" do
|
67
|
-
it "yields the value specified" do
|
68
|
-
yielded_value = Object.new
|
69
|
-
Spy.on_instance_method(klass, :existing_method).and_yield(yielded_value)
|
70
|
-
klass.new.existing_method{|value| expect(value).to be(yielded_value)}
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
context "with #and_raise" do
|
75
|
-
it "stubs a method that doesn't exist" do
|
76
|
-
Spy.on_instance_method(klass, :existing_method).and_raise(CustomErrorForAnyInstanceSpec)
|
77
|
-
expect { klass.new.existing_method}.to raise_error(CustomErrorForAnyInstanceSpec)
|
78
|
-
end
|
79
|
-
|
80
|
-
it "stubs a method that exists" do
|
81
|
-
Spy.on_instance_method(klass, :existing_method).and_raise(CustomErrorForAnyInstanceSpec)
|
82
|
-
expect { klass.new.existing_method}.to raise_error(CustomErrorForAnyInstanceSpec)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context "with a block" do
|
87
|
-
it "stubs a method" do
|
88
|
-
Spy.on_instance_method(klass, :existing_method) { 1 }
|
89
|
-
expect(klass.new.existing_method).to eq(1)
|
90
|
-
end
|
91
|
-
|
92
|
-
it "returns the same computed value for calls on different instances" do
|
93
|
-
Spy.on_instance_method(klass, :existing_method) { 1 + 2 }
|
94
|
-
expect(klass.new.existing_method).to eq(klass.new.existing_method)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context "core ruby objects" do
|
99
|
-
it "works uniformly across *everything*" do
|
100
|
-
Object.any_instance.stub(:foo).and_return(1)
|
101
|
-
expect(Object.new.foo).to eq(1)
|
102
|
-
end
|
103
|
-
|
104
|
-
it "works with the non-standard constructor []" do
|
105
|
-
Array.any_instance.stub(:foo).and_return(1)
|
106
|
-
expect([].foo).to eq(1)
|
107
|
-
end
|
108
|
-
|
109
|
-
it "works with the non-standard constructor {}" do
|
110
|
-
Hash.any_instance.stub(:foo).and_return(1)
|
111
|
-
expect({}.foo).to eq(1)
|
112
|
-
end
|
113
|
-
|
114
|
-
it "works with the non-standard constructor \"\"" do
|
115
|
-
String.any_instance.stub(:foo).and_return(1)
|
116
|
-
expect("".foo).to eq(1)
|
117
|
-
end
|
118
|
-
|
119
|
-
it "works with the non-standard constructor \'\'" do
|
120
|
-
String.any_instance.stub(:foo).and_return(1)
|
121
|
-
expect(''.foo).to eq(1)
|
122
|
-
end
|
123
|
-
|
124
|
-
it "works with the non-standard constructor module" do
|
125
|
-
Module.any_instance.stub(:foo).and_return(1)
|
126
|
-
module RSpec::SampleRspecTestModule;end
|
127
|
-
expect(RSpec::SampleRspecTestModule.foo).to eq(1)
|
128
|
-
end
|
129
|
-
|
130
|
-
it "works with the non-standard constructor class" do
|
131
|
-
Class.any_instance.stub(:foo).and_return(1)
|
132
|
-
class RSpec::SampleRspecTestClass;end
|
133
|
-
expect(RSpec::SampleRspecTestClass.foo).to eq(1)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
context "unstub implementation" do
|
139
|
-
it "replaces the stubbed method with the original method" do
|
140
|
-
Spy.on_instance_method(klass, :existing_method)
|
141
|
-
klass.any_instance.unstub(:existing_method)
|
142
|
-
expect(klass.new.existing_method).to eq(:existing_method_return_value)
|
143
|
-
end
|
144
|
-
|
145
|
-
it "removes all stubs with the supplied method name" do
|
146
|
-
Spy.on_instance_method(klass, :existing_method).with(1)
|
147
|
-
Spy.on_instance_method(klass, :existing_method).with(2)
|
148
|
-
klass.any_instance.unstub(:existing_method)
|
149
|
-
expect(klass.new.existing_method).to eq(:existing_method_return_value)
|
150
|
-
end
|
151
|
-
|
152
|
-
it "does not remove any expectations with the same method name" do
|
153
|
-
klass.any_instance.should_receive(:existing_method_with_arguments).with(3).and_return(:three)
|
154
|
-
Spy.on_instance_method(klass, :existing_method_with_arguments).with(1)
|
155
|
-
Spy.on_instance_method(klass, :existing_method_with_arguments).with(2)
|
156
|
-
klass.any_instance.unstub(:existing_method_with_arguments)
|
157
|
-
expect(klass.new.existing_method_with_arguments(3)).to eq(:three)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "raises a MockExpectationError if the method has not been stubbed" do
|
161
|
-
expect {
|
162
|
-
klass.any_instance.unstub(:existing_method)
|
163
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, 'The method `existing_method` was not stubbed or was already unstubbed')
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
context "with #should_receive" do
|
168
|
-
let(:foo_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: foo' }
|
169
|
-
let(:existing_method_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: existing_method' }
|
170
|
-
|
171
|
-
context "with an expectation is set on a method which does not exist" do
|
172
|
-
it "returns the expected value" do
|
173
|
-
klass.any_instance.should_receive(:foo).and_return(1)
|
174
|
-
expect(klass.new.foo(1)).to eq(1)
|
175
|
-
end
|
176
|
-
|
177
|
-
it "fails if an instance is created but no invocation occurs" do
|
178
|
-
expect do
|
179
|
-
klass.any_instance.should_receive(:foo)
|
180
|
-
klass.new
|
181
|
-
klass.rspec_verify
|
182
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
183
|
-
end
|
184
|
-
|
185
|
-
it "fails if no instance is created" do
|
186
|
-
expect do
|
187
|
-
klass.any_instance.should_receive(:foo).and_return(1)
|
188
|
-
klass.rspec_verify
|
189
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
190
|
-
end
|
191
|
-
|
192
|
-
it "fails if no instance is created and there are multiple expectations" do
|
193
|
-
expect do
|
194
|
-
klass.any_instance.should_receive(:foo)
|
195
|
-
klass.any_instance.should_receive(:bar)
|
196
|
-
klass.rspec_verify
|
197
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: bar, foo')
|
198
|
-
end
|
199
|
-
|
200
|
-
it "allows expectations on instances to take priority" do
|
201
|
-
klass.any_instance.should_receive(:foo)
|
202
|
-
klass.new.foo
|
203
|
-
|
204
|
-
instance = klass.new
|
205
|
-
instance.should_receive(:foo).and_return(result = Object.new)
|
206
|
-
expect(instance.foo).to eq(result)
|
207
|
-
end
|
208
|
-
|
209
|
-
context "behaves as 'exactly one instance'" do
|
210
|
-
it "passes if subsequent invocations do not receive that message" do
|
211
|
-
klass.any_instance.should_receive(:foo)
|
212
|
-
klass.new.foo
|
213
|
-
klass.new
|
214
|
-
end
|
215
|
-
|
216
|
-
it "fails if the method is invoked on a second instance" do
|
217
|
-
instance_one = klass.new
|
218
|
-
instance_two = klass.new
|
219
|
-
expect do
|
220
|
-
klass.any_instance.should_receive(:foo)
|
221
|
-
|
222
|
-
instance_one.foo
|
223
|
-
instance_two.foo
|
224
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'foo' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
context "normal expectations on the class object" do
|
229
|
-
it "fail when unfulfilled" do
|
230
|
-
expect do
|
231
|
-
klass.any_instance.should_receive(:foo)
|
232
|
-
klass.should_receive(:woot)
|
233
|
-
klass.new.foo
|
234
|
-
klass.rspec_verify
|
235
|
-
end.to(raise_error(RSpec::Mocks::MockExpectationError) do |error|
|
236
|
-
expect(error.message).not_to eq(existing_method_expectation_error_message)
|
237
|
-
end)
|
238
|
-
end
|
239
|
-
|
240
|
-
|
241
|
-
it "pass when expectations are met" do
|
242
|
-
klass.any_instance.should_receive(:foo)
|
243
|
-
klass.should_receive(:woot).and_return(result = Object.new)
|
244
|
-
klass.new.foo
|
245
|
-
expect(klass.woot).to eq(result)
|
246
|
-
end
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
context "with an expectation is set on a method that exists" do
|
251
|
-
it "returns the expected value" do
|
252
|
-
klass.any_instance.should_receive(:existing_method).and_return(1)
|
253
|
-
expect(klass.new.existing_method(1)).to eq(1)
|
254
|
-
end
|
255
|
-
|
256
|
-
it "fails if an instance is created but no invocation occurs" do
|
257
|
-
expect do
|
258
|
-
klass.any_instance.should_receive(:existing_method)
|
259
|
-
klass.new
|
260
|
-
klass.rspec_verify
|
261
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
262
|
-
end
|
263
|
-
|
264
|
-
it "fails if no instance is created" do
|
265
|
-
expect do
|
266
|
-
klass.any_instance.should_receive(:existing_method)
|
267
|
-
klass.rspec_verify
|
268
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
269
|
-
end
|
270
|
-
|
271
|
-
it "fails if no instance is created and there are multiple expectations" do
|
272
|
-
expect do
|
273
|
-
klass.any_instance.should_receive(:existing_method)
|
274
|
-
klass.any_instance.should_receive(:another_existing_method)
|
275
|
-
klass.rspec_verify
|
276
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: another_existing_method, existing_method')
|
277
|
-
end
|
278
|
-
|
279
|
-
context "after any one instance has received a message" do
|
280
|
-
it "passes if subsequent invocations do not receive that message" do
|
281
|
-
klass.any_instance.should_receive(:existing_method)
|
282
|
-
klass.new.existing_method
|
283
|
-
klass.new
|
284
|
-
end
|
285
|
-
|
286
|
-
it "fails if the method is invoked on a second instance" do
|
287
|
-
instance_one = klass.new
|
288
|
-
instance_two = klass.new
|
289
|
-
expect do
|
290
|
-
klass.any_instance.should_receive(:existing_method)
|
291
|
-
|
292
|
-
instance_one.existing_method
|
293
|
-
instance_two.existing_method
|
294
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
context "when resetting post-verification" do
|
301
|
-
let(:space) { RSpec::Mocks::Space.new }
|
302
|
-
|
303
|
-
context "existing method" do
|
304
|
-
before(:each) do
|
305
|
-
space.add(klass)
|
306
|
-
end
|
307
|
-
|
308
|
-
context "with stubbing" do
|
309
|
-
context "public methods" do
|
310
|
-
before(:each) do
|
311
|
-
Spy.on_instance_method(klass, :existing_method).and_return(1)
|
312
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_true
|
313
|
-
end
|
314
|
-
|
315
|
-
it "restores the class to its original state after each example when no instance is created" do
|
316
|
-
space.verify_all
|
317
|
-
|
318
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
319
|
-
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
320
|
-
end
|
321
|
-
|
322
|
-
it "restores the class to its original state after each example when one instance is created" do
|
323
|
-
klass.new.existing_method
|
324
|
-
|
325
|
-
space.verify_all
|
326
|
-
|
327
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
328
|
-
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
329
|
-
end
|
330
|
-
|
331
|
-
it "restores the class to its original state after each example when more than one instance is created" do
|
332
|
-
klass.new.existing_method
|
333
|
-
klass.new.existing_method
|
334
|
-
|
335
|
-
space.verify_all
|
336
|
-
|
337
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
338
|
-
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
context "private methods" do
|
343
|
-
before :each do
|
344
|
-
Spy.on_instance_method(klass, :private_method).and_return(:something)
|
345
|
-
space.verify_all
|
346
|
-
end
|
347
|
-
|
348
|
-
it "cleans up the backed up method" do
|
349
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
350
|
-
end
|
351
|
-
|
352
|
-
it "restores a stubbed private method after the spec is run" do
|
353
|
-
expect(klass.private_method_defined?(:private_method)).to be_true
|
354
|
-
end
|
355
|
-
|
356
|
-
it "ensures that the restored method behaves as it originally did" do
|
357
|
-
expect(klass.new.send(:private_method)).to eq(:private_method_return_value)
|
358
|
-
end
|
359
|
-
end
|
360
|
-
end
|
361
|
-
|
362
|
-
context "with expectations" do
|
363
|
-
context "private methods" do
|
364
|
-
before :each do
|
365
|
-
klass.any_instance.should_receive(:private_method).and_return(:something)
|
366
|
-
klass.new.private_method
|
367
|
-
space.verify_all
|
368
|
-
end
|
369
|
-
|
370
|
-
it "cleans up the backed up method" do
|
371
|
-
expect(klass.method_defined?(:__existing_method_without_any_instance__)).to be_false
|
372
|
-
end
|
373
|
-
|
374
|
-
it "restores a stubbed private method after the spec is run" do
|
375
|
-
expect(klass.private_method_defined?(:private_method)).to be_true
|
376
|
-
end
|
377
|
-
|
378
|
-
it "ensures that the restored method behaves as it originally did" do
|
379
|
-
expect(klass.new.send(:private_method)).to eq(:private_method_return_value)
|
380
|
-
end
|
381
|
-
end
|
382
|
-
|
383
|
-
context "ensures that the subsequent specs do not see expectations set in previous specs" do
|
384
|
-
context "when the instance created after the expectation is set" do
|
385
|
-
it "first spec" do
|
386
|
-
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
|
387
|
-
klass.new.existing_method
|
388
|
-
end
|
389
|
-
|
390
|
-
it "second spec" do
|
391
|
-
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
context "when the instance created before the expectation is set" do
|
396
|
-
before :each do
|
397
|
-
@instance = klass.new
|
398
|
-
end
|
399
|
-
|
400
|
-
it "first spec" do
|
401
|
-
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
|
402
|
-
@instance.existing_method
|
403
|
-
end
|
404
|
-
|
405
|
-
it "second spec" do
|
406
|
-
expect(@instance.existing_method).to eq(existing_method_return_value)
|
407
|
-
end
|
408
|
-
end
|
409
|
-
end
|
410
|
-
|
411
|
-
it "ensures that the next spec does not see that expectation" do
|
412
|
-
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
|
413
|
-
klass.new.existing_method
|
414
|
-
space.verify_all
|
415
|
-
|
416
|
-
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
417
|
-
end
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
|
-
context "with multiple calls to any_instance in the same example" do
|
422
|
-
it "does not prevent the change from being rolled back" do
|
423
|
-
Spy.on_instance_method(klass, :existing_method).and_return(false)
|
424
|
-
Spy.on_instance_method(klass, :existing_method).and_return(true)
|
425
|
-
|
426
|
-
klass.rspec_verify
|
427
|
-
expect(klass.new).to respond_to(:existing_method)
|
428
|
-
expect(klass.new.existing_method).to eq(existing_method_return_value)
|
429
|
-
end
|
430
|
-
end
|
431
|
-
|
432
|
-
it "adds an class to the current space when #any_instance is invoked" do
|
433
|
-
klass.any_instance
|
434
|
-
expect(RSpec::Mocks::space.send(:receivers)).to include(klass)
|
435
|
-
end
|
436
|
-
|
437
|
-
it "adds an instance to the current space when stubbed method is invoked" do
|
438
|
-
Spy.on_instance_method(klass, :foo)
|
439
|
-
instance = klass.new
|
440
|
-
instance.foo
|
441
|
-
expect(RSpec::Mocks::space.send(:receivers)).to include(instance)
|
442
|
-
end
|
443
|
-
end
|
444
|
-
|
445
|
-
context 'when used in conjunction with a `dup`' do
|
446
|
-
it "doesn't cause an infinite loop" do
|
447
|
-
Spy::Subroutine.new(Object, :some_method, false).hook(force: true)
|
448
|
-
o = Object.new
|
449
|
-
o.some_method
|
450
|
-
expect { o.dup.some_method }.to_not raise_error(SystemStackError)
|
451
|
-
end
|
452
|
-
|
453
|
-
it "doesn't bomb if the object doesn't support `dup`" do
|
454
|
-
klass = Class.new do
|
455
|
-
undef_method :dup
|
456
|
-
end
|
457
|
-
Spy::Subroutine.new(Object, :some_method, false).hook(force: true)
|
458
|
-
end
|
459
|
-
|
460
|
-
it "doesn't fail when dup accepts parameters" do
|
461
|
-
klass = Class.new do
|
462
|
-
def dup(funky_option)
|
463
|
-
end
|
464
|
-
end
|
465
|
-
|
466
|
-
Spy::Subroutine.new(Object, :some_method, false).hook(force: true)
|
467
|
-
|
468
|
-
expect { klass.new.dup('Dup dup dup') }.to_not raise_error(ArgumentError)
|
469
|
-
end
|
470
|
-
end
|
471
|
-
|
472
|
-
context "when directed at a method defined on a superclass" do
|
473
|
-
let(:sub_klass) { Class.new(klass) }
|
474
|
-
|
475
|
-
it "stubs the method correctly" do
|
476
|
-
Spy.on_instance_method(klass, :existing_method).and_return("foo")
|
477
|
-
expect(sub_klass.new.existing_method).to eq "foo"
|
478
|
-
end
|
479
|
-
|
480
|
-
it "mocks the method correctly" do
|
481
|
-
instance_one = sub_klass.new
|
482
|
-
instance_two = sub_klass.new
|
483
|
-
expect do
|
484
|
-
klass.any_instance.should_receive(:existing_method)
|
485
|
-
instance_one.existing_method
|
486
|
-
instance_two.existing_method
|
487
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
|
488
|
-
end
|
489
|
-
end
|
490
|
-
|
491
|
-
context "when a class overrides Object#method" do
|
492
|
-
let(:http_request_class) { Struct.new(:method, :uri) }
|
493
|
-
|
494
|
-
it "stubs the method correctly" do
|
495
|
-
http_request_class.any_instance.stub(:existing_method).and_return("foo")
|
496
|
-
expect(http_request_class.new.existing_method).to eq "foo"
|
497
|
-
end
|
498
|
-
|
499
|
-
it "mocks the method correctly" do
|
500
|
-
http_request_class.any_instance.should_receive(:existing_method).and_return("foo")
|
501
|
-
expect(http_request_class.new.existing_method).to eq "foo"
|
502
|
-
end
|
503
|
-
end
|
504
|
-
|
505
|
-
context "when used after the test has finished" do
|
506
|
-
it "restores the original behavior of a stubbed method" do
|
507
|
-
Spy.on_instance_method(klass, :existing_method).and_return(:stubbed_return_value)
|
508
|
-
|
509
|
-
instance = klass.new
|
510
|
-
expect(instance.existing_method).to eq :stubbed_return_value
|
511
|
-
|
512
|
-
RSpec::Mocks.verify
|
513
|
-
|
514
|
-
expect(instance.existing_method).to eq :existing_method_return_value
|
515
|
-
end
|
516
|
-
end
|
517
|
-
end
|
518
|
-
end
|
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Spy
|
4
|
-
describe "Hash excluding matchers", broken: true do
|
5
|
-
|
6
|
-
it "describes itself properly" do
|
7
|
-
expect(HashExcludingMatcher.new(:a => 5).description).to eq "hash_not_including(:a=>5)"
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "passing" do
|
11
|
-
it "matches a hash without the specified key" do
|
12
|
-
expect(hash_not_including(:c)).to eq({:a => 1, :b => 2})
|
13
|
-
end
|
14
|
-
|
15
|
-
it "matches a hash with the specified key, but different value" do
|
16
|
-
expect(hash_not_including(:b => 3)).to eq({:a => 1, :b => 2})
|
17
|
-
end
|
18
|
-
|
19
|
-
it "matches a hash without the specified key, given as anything()" do
|
20
|
-
expect(hash_not_including(:c => anything)).to eq({:a => 1, :b => 2})
|
21
|
-
end
|
22
|
-
|
23
|
-
it "matches an empty hash" do
|
24
|
-
expect(hash_not_including(:a)).to eq({})
|
25
|
-
end
|
26
|
-
|
27
|
-
it "matches a hash without any of the specified keys" do
|
28
|
-
expect(hash_not_including(:a, :b, :c)).to eq(:d => 7)
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "failing" do
|
34
|
-
it "does not match a non-hash" do
|
35
|
-
expect(hash_not_including(:a => 1)).not_to eq 1
|
36
|
-
end
|
37
|
-
|
38
|
-
it "does not match a hash with a specified key" do
|
39
|
-
expect(hash_not_including(:b)).not_to eq(:b => 2)
|
40
|
-
end
|
41
|
-
|
42
|
-
it "does not match a hash with the specified key/value pair" do
|
43
|
-
expect(hash_not_including(:b => 2)).not_to eq(:a => 1, :b => 2)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "does not match a hash with the specified key" do
|
47
|
-
expect(hash_not_including(:a, :b => 3)).not_to eq(:a => 1, :b => 2)
|
48
|
-
end
|
49
|
-
|
50
|
-
it "does not match a hash with one of the specified keys" do
|
51
|
-
expect(hash_not_including(:a, :b)).not_to eq(:b => 2)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "does not match a hash with some of the specified keys" do
|
55
|
-
expect(hash_not_including(:a, :b, :c)).not_to eq(:a => 1, :b => 2)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "does not match a hash with one key/value pair included" do
|
59
|
-
expect(hash_not_including(:a, :b, :c, :d => 7)).not_to eq(:d => 7)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,86 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Spy
|
4
|
-
describe "Hash including matchers", broken: true do
|
5
|
-
|
6
|
-
it "describes itself properly" do
|
7
|
-
expect(HashIncludingMatcher.new(:a => 1).description).to eq "hash_including(:a=>1)"
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "passing" do
|
11
|
-
it "matches the same hash" do
|
12
|
-
expect(hash_including(:a => 1)).to eq({:a => 1})
|
13
|
-
end
|
14
|
-
|
15
|
-
it "matches a hash with extra stuff" do
|
16
|
-
expect(hash_including(:a => 1)).to eq({:a => 1, :b => 2})
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "when matching against other matchers" do
|
20
|
-
it "matches an int against anything()" do
|
21
|
-
expect(hash_including(:a => anything, :b => 2)).to eq({:a => 1, :b => 2})
|
22
|
-
end
|
23
|
-
|
24
|
-
it "matches a string against anything()" do
|
25
|
-
expect(hash_including(:a => anything, :b => 2)).to eq({:a => "1", :b => 2})
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
describe "when passed only keys or keys mixed with key/value pairs" do
|
30
|
-
it "matches if the key is present" do
|
31
|
-
expect(hash_including(:a)).to eq({:a => 1, :b => 2})
|
32
|
-
end
|
33
|
-
|
34
|
-
it "matches if more keys are present" do
|
35
|
-
expect(hash_including(:a, :b)).to eq({:a => 1, :b => 2, :c => 3})
|
36
|
-
end
|
37
|
-
|
38
|
-
it "matches a string against a given key" do
|
39
|
-
expect(hash_including(:a)).to eq({:a => "1", :b => 2})
|
40
|
-
end
|
41
|
-
|
42
|
-
it "matches if passed one key and one key/value pair" do
|
43
|
-
expect(hash_including(:a, :b => 2)).to eq({:a => 1, :b => 2})
|
44
|
-
end
|
45
|
-
|
46
|
-
it "matches if passed many keys and one key/value pair" do
|
47
|
-
expect(hash_including(:a, :b, :c => 3)).to eq({:a => 1, :b => 2, :c => 3, :d => 4})
|
48
|
-
end
|
49
|
-
|
50
|
-
it "matches if passed many keys and many key/value pairs" do
|
51
|
-
expect(hash_including(:a, :b, :c => 3, :e => 5)).to eq({:a => 1, :b => 2, :c => 3, :d => 4, :e => 5})
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe "failing" do
|
57
|
-
it "does not match a non-hash" do
|
58
|
-
expect(hash_including(:a => 1)).not_to eq 1
|
59
|
-
end
|
60
|
-
|
61
|
-
it "does not match a hash with a missing key" do
|
62
|
-
expect(hash_including(:a => 1)).not_to eq(:b => 2)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "does not match a hash with a missing key" do
|
66
|
-
expect(hash_including(:a)).not_to eq(:b => 2)
|
67
|
-
end
|
68
|
-
|
69
|
-
it "does not match an empty hash with a given key" do
|
70
|
-
expect(hash_including(:a)).not_to eq({})
|
71
|
-
end
|
72
|
-
|
73
|
-
it "does not match a hash with a missing key when one pair is matching" do
|
74
|
-
expect(hash_including(:a, :b => 2)).not_to eq(:b => 2)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "does not match a hash with an incorrect value" do
|
78
|
-
expect(hash_including(:a => 1, :b => 2)).not_to eq(:a => 1, :b => 3)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "does not match when values are nil but keys are different" do
|
82
|
-
expect(hash_including(:a => nil)).not_to eq(:b => nil)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|