forked from rspec/rspec-mocks
/
any_instance_spec.rb
754 lines (634 loc) · 29.8 KB
/
any_instance_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
require 'spec_helper'
module RSpec
module Mocks
describe "#any_instance" do
class CustomErrorForAnyInstanceSpec < StandardError;end
let(:klass) do
Class.new do
def existing_method; :existing_method_return_value; end
def existing_method_with_arguments(arg_one, arg_two = nil); :existing_method_with_arguments_return_value; end
def another_existing_method; end
private
def private_method; :private_method_return_value; end
end
end
let(:existing_method_return_value){ :existing_method_return_value }
context "invocation order" do
context "#stub" do
it "raises an error if 'stub' follows 'with'" do
lambda{ klass.any_instance.with("1").stub(:foo) }.should raise_error(NoMethodError)
end
it "raises an error if 'with' follows 'and_return'" do
lambda{ klass.any_instance.stub(:foo).and_return(1).with("1") }.should raise_error(NoMethodError)
end
it "raises an error if 'with' follows 'and_raise'" do
lambda{ klass.any_instance.stub(:foo).and_raise(1).with("1") }.should raise_error(NoMethodError)
end
it "raises an error if 'with' follows 'and_yield'" do
lambda{ klass.any_instance.stub(:foo).and_yield(1).with("1") }.should raise_error(NoMethodError)
end
end
context "#stub_chain" do
it "raises an error if 'stub_chain' follows 'any_instance'" do
lambda{ klass.any_instance.and_return("1").stub_chain(:foo, :bar) }.should raise_error(NoMethodError)
end
end
context "#should_receive" do
it "raises an error if 'should_receive' follows 'with'" do
lambda{ klass.any_instance.with("1").should_receive(:foo) }.should raise_error(NoMethodError)
end
it "raises an error if 'with' follows 'and_return'" do
pending "see Github issue #42"
lambda{ klass.any_instance.should_receive(:foo).and_return(1).with("1") }.should raise_error(NoMethodError)
end
it "raises an error if 'with' follows 'and_raise'" do
pending "see Github issue #42"
lambda{ klass.any_instance.should_receive(:foo).and_raise(1).with("1") }.should raise_error(NoMethodError)
end
end
end
context "with #stub" do
it "does not suppress an exception when a method that doesn't exist is invoked" do
klass.any_instance.stub(:foo)
lambda{ klass.new.bar }.should raise_error(NoMethodError)
end
context 'multiple methods' do
it "allows multiple methods to be stubbed in a single invocation" do
klass.any_instance.stub(:foo => 'foo', :bar => 'bar')
instance = klass.new
instance.foo.should eq('foo')
instance.bar.should eq('bar')
end
it "adheres to the contract of multiple method stubbing withou any instance" do
Object.new.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
klass.any_instance.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
end
context "allows a chain of methods to be stubbed using #stub_chain" do
it "given symbols representing the methods" do
klass.any_instance.stub_chain(:one, :two, :three).and_return(:four)
klass.new.one.two.three.should eq(:four)
end
it "given a hash as the last argument uses the value as the expected return value" do
klass.any_instance.stub_chain(:one, :two, :three => :four)
klass.new.one.two.three.should eq(:four)
end
it "given a string of '.' separated method names representing the chain" do
klass.any_instance.stub_chain('one.two.three').and_return(:four)
klass.new.one.two.three.should eq(:four)
end
end
end
context "behaves as 'every instance'" do
it "stubs every instance in the spec" do
klass.any_instance.stub(:foo).and_return(result = Object.new)
klass.new.foo.should eq(result)
klass.new.foo.should eq(result)
end
it "stubs instance created before any_instance was called" do
instance = klass.new
klass.any_instance.stub(:foo).and_return(result = Object.new)
instance.foo.should eq(result)
end
end
context "with argument matching" do
before do
klass.any_instance.stub(:foo).with(:param_one, :param_two).and_return(:result_one)
klass.any_instance.stub(:foo).with(:param_three, :param_four).and_return(:result_two)
end
it "returns the stubbed value when arguments match" do
instance = klass.new
instance.foo(:param_one, :param_two).should eq(:result_one)
instance.foo(:param_three, :param_four).should eq(:result_two)
end
it "fails the spec with an expectation error when the arguments do not match" do
expect do
klass.new.foo(:param_one, :param_three)
end.to(raise_error(RSpec::Mocks::MockExpectationError))
end
end
context "with multiple stubs" do
before do
klass.any_instance.stub(:foo).and_return(1)
klass.any_instance.stub(:bar).and_return(2)
end
it "stubs a method" do
instance = klass.new
instance.foo.should eq(1)
instance.bar.should eq(2)
end
it "returns the same value for calls on different instances" do
klass.new.foo.should eq(klass.new.foo)
klass.new.bar.should eq(klass.new.bar)
end
end
context "with #and_return" do
it "stubs a method that doesn't exist" do
klass.any_instance.stub(:foo).and_return(1)
klass.new.foo.should eq(1)
end
it "stubs a method that exists" do
klass.any_instance.stub(:existing_method).and_return(1)
klass.new.existing_method.should eq(1)
end
it "returns the same object for calls on different instances" do
return_value = Object.new
klass.any_instance.stub(:foo).and_return(return_value)
klass.new.foo.should be(return_value)
klass.new.foo.should be(return_value)
end
end
context "with #and_yield" do
it "yields the value specified" do
yielded_value = Object.new
klass.any_instance.stub(:foo).and_yield(yielded_value)
klass.new.foo{|value| value.should be(yielded_value)}
end
end
context "with #and_raise" do
it "stubs a method that doesn't exist" do
klass.any_instance.stub(:foo).and_raise(CustomErrorForAnyInstanceSpec)
lambda{ klass.new.foo}.should raise_error(CustomErrorForAnyInstanceSpec)
end
it "stubs a method that exists" do
klass.any_instance.stub(:existing_method).and_raise(CustomErrorForAnyInstanceSpec)
lambda{ klass.new.existing_method}.should raise_error(CustomErrorForAnyInstanceSpec)
end
end
context "with a block" do
it "stubs a method" do
klass.any_instance.stub(:foo) { 1 }
klass.new.foo.should eq(1)
end
it "returns the same computed value for calls on different instances" do
klass.any_instance.stub(:foo) { 1 + 2 }
klass.new.foo.should eq(klass.new.foo)
end
end
context "core ruby objects" do
it "works uniformly across *everything*" do
Object.any_instance.stub(:foo).and_return(1)
Object.new.foo.should eq(1)
end
it "works with the non-standard constructor []" do
Array.any_instance.stub(:foo).and_return(1)
[].foo.should eq(1)
end
it "works with the non-standard constructor {}" do
Hash.any_instance.stub(:foo).and_return(1)
{}.foo.should eq(1)
end
it "works with the non-standard constructor \"\"" do
String.any_instance.stub(:foo).and_return(1)
"".foo.should eq(1)
end
it "works with the non-standard constructor \'\'" do
String.any_instance.stub(:foo).and_return(1)
''.foo.should eq(1)
end
it "works with the non-standard constructor module" do
Module.any_instance.stub(:foo).and_return(1)
module RSpec::SampleRspecTestModule;end
RSpec::SampleRspecTestModule.foo.should eq(1)
end
it "works with the non-standard constructor class" do
Class.any_instance.stub(:foo).and_return(1)
class RSpec::SampleRspecTestClass;end
RSpec::SampleRspecTestClass.foo.should eq(1)
end
end
end
context "with #stub!" do
it "raises with a message instructing the user to use stub instead" do
expect do
klass.any_instance.stub!(:foo)
end.to raise_error(/Use stub instead/)
end
end
context "unstub implementation" do
it "replaces the stubbed method with the original method" do
klass.any_instance.stub(:existing_method)
klass.any_instance.unstub(:existing_method)
klass.new.existing_method.should eq(:existing_method_return_value)
end
it "removes all stubs with the supplied method name" do
klass.any_instance.stub(:existing_method).with(1)
klass.any_instance.stub(:existing_method).with(2)
klass.any_instance.unstub(:existing_method)
klass.new.existing_method.should eq(:existing_method_return_value)
end
it "does not remove any expectations with the same method name" do
klass.any_instance.should_receive(:existing_method_with_arguments).with(3).and_return(:three)
klass.any_instance.stub(:existing_method_with_arguments).with(1)
klass.any_instance.stub(:existing_method_with_arguments).with(2)
klass.any_instance.unstub(:existing_method_with_arguments)
klass.new.existing_method_with_arguments(3).should eq :three
end
it "raises a MockExpectationError if the method has not been stubbed" do
lambda do
klass.any_instance.unstub(:existing_method)
end.should raise_error(RSpec::Mocks::MockExpectationError, 'The method `existing_method` was not stubbed or was already unstubbed')
end
end
context "with #should_receive" do
let(:foo_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: foo' }
let(:existing_method_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: existing_method' }
context "with an expectation is set on a method which does not exist" do
it "returns the expected value" do
klass.any_instance.should_receive(:foo).and_return(1)
klass.new.foo(1).should eq(1)
end
it "fails if an instance is created but no invocation occurs" do
expect do
klass.any_instance.should_receive(:foo)
klass.new
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails if no instance is created" do
expect do
klass.any_instance.should_receive(:foo).and_return(1)
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails if no instance is created and there are multiple expectations" do
expect do
klass.any_instance.should_receive(:foo)
klass.any_instance.should_receive(:bar)
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: bar, foo')
end
it "allows expectations on instances to take priority" do
klass.any_instance.should_receive(:foo)
klass.new.foo
instance = klass.new
instance.should_receive(:foo).and_return(result = Object.new)
instance.foo.should eq(result)
end
context "behaves as 'exactly one instance'" do
it "passes if subsequent invocations do not receive that message" do
klass.any_instance.should_receive(:foo)
klass.new.foo
klass.new
end
it "fails if the method is invoked on a second instance" do
instance_one = klass.new
instance_two = klass.new
expect do
klass.any_instance.should_receive(:foo)
instance_one.foo
instance_two.foo
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}")
end
end
context "normal expectations on the class object" do
it "fail when unfulfilled" do
expect do
klass.any_instance.should_receive(:foo)
klass.should_receive(:woot)
klass.new.foo
klass.rspec_verify
end.to(raise_error(RSpec::Mocks::MockExpectationError) do |error|
error.message.should_not eq(existing_method_expectation_error_message)
end)
end
it "pass when expectations are met" do
klass.any_instance.should_receive(:foo)
klass.should_receive(:woot).and_return(result = Object.new)
klass.new.foo
klass.woot.should eq(result)
end
end
end
context "with an expectation is set on a method that exists" do
it "returns the expected value" do
klass.any_instance.should_receive(:existing_method).and_return(1)
klass.new.existing_method(1).should eq(1)
end
it "fails if an instance is created but no invocation occurs" do
expect do
klass.any_instance.should_receive(:existing_method)
klass.new
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
it "fails if no instance is created" do
expect do
klass.any_instance.should_receive(:existing_method)
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
it "fails if no instance is created and there are multiple expectations" do
expect do
klass.any_instance.should_receive(:existing_method)
klass.any_instance.should_receive(:another_existing_method)
klass.rspec_verify
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')
end
context "after any one instance has received a message" do
it "passes if subsequent invocations do not receive that message" do
klass.any_instance.should_receive(:existing_method)
klass.new.existing_method
klass.new
end
it "fails if the method is invoked on a second instance" do
instance_one = klass.new
instance_two = klass.new
expect do
klass.any_instance.should_receive(:existing_method)
instance_one.existing_method
instance_two.existing_method
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}")
end
end
end
context "with argument matching" do
before do
klass.any_instance.should_receive(:foo).with(:param_one, :param_two).and_return(:result_one)
klass.any_instance.should_receive(:foo).with(:param_three, :param_four).and_return(:result_two)
end
it "returns the expected value when arguments match" do
instance = klass.new
instance.foo(:param_one, :param_two).should eq(:result_one)
instance.foo(:param_three, :param_four).should eq(:result_two)
end
it "fails when the arguments match but different instances are used" do
instances = Array.new(2) { klass.new }
expect do
instances[0].foo(:param_one, :param_two).should eq(:result_one)
instances[1].foo(:param_three, :param_four).should eq(:result_two)
end.to raise_error(RSpec::Mocks::MockExpectationError)
# ignore the fact that should_receive expectations were not met
instances.each { |instance| instance.rspec_reset }
end
it "is not affected by the invocation of existing methods on other instances" do
klass.new.existing_method_with_arguments(:param_one, :param_two).should eq(:existing_method_with_arguments_return_value)
instance = klass.new
instance.foo(:param_one, :param_two).should eq(:result_one)
instance.foo(:param_three, :param_four).should eq(:result_two)
end
it "fails when arguments do not match" do
instance = klass.new
expect do
instance.foo(:param_one, :param_three)
end.to raise_error(RSpec::Mocks::MockExpectationError)
# ignore the fact that should_receive expectations were not met
instance.rspec_reset
end
end
context "message count" do
context "the 'once' constraint" do
it "passes for one invocation" do
klass.any_instance.should_receive(:foo).once
klass.new.foo
end
it "fails when no instances are declared" do
expect do
klass.any_instance.should_receive(:foo).once
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails when an instance is declared but there are no invocations" do
expect do
klass.any_instance.should_receive(:foo).once
klass.new
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
end
it "fails for more than one invocation" do
expect do
klass.any_instance.should_receive(:foo).once
instance = klass.new
2.times { instance.foo }
instance.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError)
end
end
context "the 'twice' constraint" do
it "passes for two invocations" do
klass.any_instance.should_receive(:foo).twice
instance = klass.new
2.times { instance.foo }
end
it "fails for more than two invocations" do
expect do
klass.any_instance.should_receive(:foo).twice
instance = klass.new
3.times { instance.foo }
instance.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError)
end
end
context "the 'exactly(n)' constraint" do
it "passes for n invocations where n = 3" do
klass.any_instance.should_receive(:foo).exactly(3).times
instance = klass.new
3.times { instance.foo }
end
it "fails for n invocations where n < 3" do
expect do
klass.any_instance.should_receive(:foo).exactly(3).times
instance = klass.new
2.times { instance.foo }
instance.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError)
end
it "fails for n invocations where n > 3" do
expect do
klass.any_instance.should_receive(:foo).exactly(3).times
instance = klass.new
4.times { instance.foo }
instance.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError)
end
end
context "the 'at_least(n)' constraint" do
it "passes for n invocations where n = 3" do
klass.any_instance.should_receive(:foo).at_least(3).times
instance = klass.new
3.times { instance.foo }
end
it "fails for n invocations where n < 3" do
expect do
klass.any_instance.should_receive(:foo).at_least(3).times
instance = klass.new
2.times { instance.foo }
instance.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError)
end
it "passes for n invocations where n > 3" do
klass.any_instance.should_receive(:foo).at_least(3).times
instance = klass.new
4.times { instance.foo }
end
end
context "the 'at_most(n)' constraint" do
it "passes for n invocations where n = 3" do
klass.any_instance.should_receive(:foo).at_most(3).times
instance = klass.new
3.times { instance.foo }
end
it "passes for n invocations where n < 3" do
klass.any_instance.should_receive(:foo).at_most(3).times
instance = klass.new
2.times { instance.foo }
end
it "fails for n invocations where n > 3" do
expect do
klass.any_instance.should_receive(:foo).at_most(3).times
instance = klass.new
4.times { instance.foo }
instance.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError)
end
end
context "the 'never' constraint" do
it "passes for 0 invocations" do
klass.any_instance.should_receive(:foo).never
klass.rspec_verify
end
it "fails on the first invocation" do
expect do
klass.any_instance.should_receive(:foo).never
klass.new.foo
end.to raise_error(RSpec::Mocks::MockExpectationError)
end
context "when combined with other expectations" do
it "passes when the other expecations are met" do
klass.any_instance.should_receive(:foo).never
klass.any_instance.should_receive(:existing_method).and_return(5)
klass.new.existing_method.should eq(5)
end
it "fails when the other expecations are not met" do
expect do
klass.any_instance.should_receive(:foo).never
klass.any_instance.should_receive(:existing_method).and_return(5)
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
end
end
context "the 'any_number_of_times' constraint" do
it "passes for 0 invocations" do
klass.any_instance.should_receive(:foo).any_number_of_times
klass.new.rspec_verify
end
it "passes for a non-zero number of invocations" do
klass.any_instance.should_receive(:foo).any_number_of_times
instance = klass.new
instance.foo
instance.rspec_verify
end
it "does not interfere with other expectations" do
klass.any_instance.should_receive(:foo).any_number_of_times
klass.any_instance.should_receive(:existing_method).and_return(5)
klass.new.existing_method.should eq(5)
end
context "when combined with other expectations" do
it "passes when the other expecations are met" do
klass.any_instance.should_receive(:foo).any_number_of_times
klass.any_instance.should_receive(:existing_method).and_return(5)
klass.new.existing_method.should eq(5)
end
it "fails when the other expecations are not met" do
expect do
klass.any_instance.should_receive(:foo).any_number_of_times
klass.any_instance.should_receive(:existing_method).and_return(5)
klass.rspec_verify
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
end
end
end
end
end
context "when resetting post-verification" do
let(:space) { RSpec::Mocks::Space.new }
context "existing method" do
before(:each) do
space.add(klass)
end
context "with stubbing" do
before(:each) do
klass.any_instance.stub(:existing_method).and_return(1)
klass.method_defined?(:__existing_method_without_any_instance__).should be_true
end
it "restores the class to its original state after each example when no instance is created" do
space.verify_all
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
klass.new.existing_method.should eq(existing_method_return_value)
end
it "restores the class to its original state after each example when one instance is created" do
klass.new.existing_method
space.verify_all
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
klass.new.existing_method.should eq(existing_method_return_value)
end
it "restores the class to its original state after each example when more than one instance is created" do
klass.new.existing_method
klass.new.existing_method
space.verify_all
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
klass.new.existing_method.should eq(existing_method_return_value)
end
context "private methods" do
it "restores a stubbed private method after the spec is run (issue #85)" do
klass.any_instance.stub(:private_method).and_return(:something)
space.verify_all
klass.new.send(:private_method).should eq(:private_method_return_value)
end
end
end
context "with expectations" do
context "ensures that the subsequent specs do not see expectations set in previous specs" do
context "when the instance created after the expectation is set" do
it "first spec" do
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
klass.new.existing_method
end
it "second spec" do
klass.new.existing_method.should eq(existing_method_return_value)
end
end
context "when the instance created before the expectation is set" do
before :each do
@instance = klass.new
end
it "first spec" do
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
@instance.existing_method
end
it "second spec" do
@instance.existing_method.should eq(existing_method_return_value)
end
end
end
it "ensures that the next spec does not see that expectation" do
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
klass.new.existing_method
space.verify_all
klass.new.existing_method.should eq(existing_method_return_value)
end
end
end
context "with multiple calls to any_instance in the same example" do
it "does not prevent the change from being rolled back" do
klass.any_instance.stub(:existing_method).and_return(false)
klass.any_instance.stub(:existing_method).and_return(true)
klass.rspec_verify
klass.new.should respond_to(:existing_method)
klass.new.existing_method.should eq(existing_method_return_value)
end
end
it "adds an class to the current space when #any_instance is invoked" do
klass.any_instance
RSpec::Mocks::space.send(:mocks).should include(klass)
end
it "adds an instance to the current space when stubbed method is invoked" do
klass.any_instance.stub(:foo)
instance = klass.new
instance.foo
RSpec::Mocks::space.send(:mocks).should include(instance)
end
end
end
end
end