File: mock_spec.rb

package info (click to toggle)
ruby-rspec-mocks 2.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 868 kB
  • ctags: 725
  • sloc: ruby: 8,227; makefile: 4
file content (798 lines) | stat: -rw-r--r-- 29,789 bytes parent folder | download | duplicates (2)
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
require 'spec_helper'

module RSpec
  module Mocks
    describe Mock do
      before(:each) { @double = double("test double") }
      after(:each)  { reset @double }

      it "has method_missing as private" do
        expect(RSpec::Mocks::Mock.private_instance_methods).to include_method(:method_missing)
      end

      it "does not respond_to? method_missing (because it's private)" do
        expect(RSpec::Mocks::Mock.new).not_to respond_to(:method_missing)
      end

      it "reports line number of expectation of unreceived message" do
        expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
        begin
          verify @double
          violated
        rescue RSpec::Mocks::MockExpectationError => e
          # NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
          expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
        end
      end

      it "reports line number of expectation of unreceived message after #should_receive after similar stub" do
        @double.stub(:wont_happen)
        expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
        begin
          verify @double
          violated
        rescue RSpec::Mocks::MockExpectationError => e
          # NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
          expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
        end
      end

      it "passes when not receiving message specified as not to be received" do
        @double.should_not_receive(:not_expected)
        verify @double
      end

      it "prevents confusing double-negative expressions involving `never`" do
        expect {
          @double.should_not_receive(:not_expected).never
        }.to raise_error(/trying to negate it again/)
      end

      def expect_and_return_warning
        expect(RSpec).to receive(:deprecate).with(/`and_return` on a negative message expectation/)
      end

      it "warns when `should_not_receive().and_return` is used" do
        expect_and_return_warning
        @double.should_not_receive(:foo).and_return(1)
      end

      it "warns when `should_receive().never.and_return` is used" do
        expect_and_return_warning
        @double.should_receive(:foo).never.and_return(1)
      end

      it "warns when `expect().not_to receive().and_return` is used" do
        expect_and_return_warning
        expect(@double).not_to receive(:foo).and_return(1)
      end

      it "warns when `expect().to receive().never.and_return` is used" do
        expect_and_return_warning
        expect(@double).to receive(:foo).never.and_return(1)
      end

      it "passes when receiving message specified as not to be received with different args" do
        @double.should_not_receive(:message).with("unwanted text")
        @double.should_receive(:message).with("other text")
        @double.message "other text"
        verify @double
      end

      it "fails when receiving message specified as not to be received" do
        @double.should_not_receive(:not_expected)
        expect {
          @double.not_expected
          violated
        }.to raise_error(
          RSpec::Mocks::MockExpectationError,
          %Q|(Double "test double").not_expected(no args)\n    expected: 0 times with any arguments\n    received: 1 time|
        )
      end

      it "fails when receiving message specified as not to be received with args" do
        @double.should_not_receive(:not_expected).with("unexpected text")
        expect {
          @double.not_expected("unexpected text")
          violated
        }.to raise_error(
          RSpec::Mocks::MockExpectationError,
          %Q|(Double "test double").not_expected("unexpected text")\n    expected: 0 times with arguments: ("unexpected text")\n    received: 1 time with arguments: ("unexpected text")|
        )
      end

      it "fails when array arguments do not match" do
        @double.should_not_receive(:not_expected).with(["do not want"])
        expect {
          @double.not_expected(["do not want"])
          violated
        }.to raise_error(
          RSpec::Mocks::MockExpectationError,
          %Q|(Double "test double").not_expected(["do not want"])\n    expected: 0 times with arguments: (["do not want"])\n    received: 1 time with arguments: (["do not want"])|
        )
      end

      it "passes when receiving message specified as not to be received with wrong args" do
        @double.should_not_receive(:not_expected).with("unexpected text")
        @double.not_expected "really unexpected text"
        verify @double

        @double.should_receive(:not_expected).with("unexpected text").never
        @double.not_expected "really unexpected text"
        verify @double
      end

      it "allows block to calculate return values" do
        @double.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
        expect(@double.something("a","b","c")).to eq "cba"
        verify @double
      end

      it "allows parameter as return value" do
        @double.should_receive(:something).with("a","b","c").and_return("booh")
        expect(@double.something("a","b","c")).to eq "booh"
        verify @double
      end

      it "returns the previously stubbed value if no return value is set" do
        @double.stub(:something).with("a","b","c").and_return(:stubbed_value)
        @double.should_receive(:something).with("a","b","c")
        expect(@double.something("a","b","c")).to eq :stubbed_value
        verify @double
      end

      it "returns nil if no return value is set and there is no previously stubbed value" do
        @double.should_receive(:something).with("a","b","c")
        expect(@double.something("a","b","c")).to be_nil
        verify @double
      end

      it "raises exception if args don't match when method called" do
        @double.should_receive(:something).with("a","b","c").and_return("booh")
        expect {
          @double.something("a","d","c")
          violated
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n  expected: (\"a\", \"b\", \"c\")\n       got: (\"a\", \"d\", \"c\")")
      end

      describe "even when a similar expectation with different arguments exist" do
        it "raises exception if args don't match when method called, correctly reporting the offending arguments" do
          @double.should_receive(:something).with("a","b","c").once
          @double.should_receive(:something).with("z","x","c").once
          expect {
            @double.something("a","b","c")
            @double.something("z","x","g")
          }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n  expected: (\"z\", \"x\", \"c\")\n       got: (\"z\", \"x\", \"g\")")
        end
      end

      it "raises exception if args don't match when method called even when the method is stubbed" do
        @double.stub(:something)
        @double.should_receive(:something).with("a","b","c")
        expect {
          @double.something("a","d","c")
          verify @double
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n  expected: (\"a\", \"b\", \"c\")\n       got: (\"a\", \"d\", \"c\")")
      end

      it "raises exception if args don't match when method called even when using null_object" do
        @double = double("test double").as_null_object
        @double.should_receive(:something).with("a","b","c")
        expect {
          @double.something("a","d","c")
          verify @double
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n  expected: (\"a\", \"b\", \"c\")\n       got: (\"a\", \"d\", \"c\")")
      end

      describe 'with a method that has a default argument' do
        it "raises an exception if the arguments don't match when the method is called, correctly reporting the offending arguments" do
          def @double.method_with_default_argument(arg={}); end
          @double.should_receive(:method_with_default_argument).with({})

          expect {
            @double.method_with_default_argument(nil)
            verify @double
          }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :method_with_default_argument with unexpected arguments\n  expected: ({})\n       got: (nil)")
        end
      end

      it "fails if unexpected method called" do
        expect {
          @double.something("a","b","c")
          violated
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received unexpected message :something with (\"a\", \"b\", \"c\")")
      end

      it "uses block for expectation if provided" do
        @double.should_receive(:something) do | a, b |
          expect(a).to eq "a"
          expect(b).to eq "b"
          "booh"
        end
        expect(@double.something("a", "b")).to eq "booh"
        verify @double
      end

      it "fails if expectation block fails" do
        @double.should_receive(:something) do |bool|
          expect(bool).to be_true
        end

        expect {
          @double.something false
        }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
      end

      it "is wrappable in an array" do
        expect( Array(@double) ).to eq([@double])
      end

      it "is wrappable in an array when a null object" do
        expect( Array(@double.as_null_object) ).to eq [@double]
      end

      it "responds to to_ary as a null object" do
        expect(@double.as_null_object.to_ary).to eq nil
      end

      it "responds to to_a as a null object" do
        if RUBY_VERSION.to_f > 1.8
          expect(@double.as_null_object.to_a).to eq nil
        else
          expect(@double.as_null_object.to_a).to eq [@double]
        end
      end

      it "passes proc to expectation block without an argument" do
        @double.should_receive(:foo) { |&block| expect(block.call).to eq(:bar) }
        @double.foo { :bar }
      end

      context "with Ruby > 1.8.6", :unless => RUBY_VERSION.to_s == '1.8.6' do
        it "passes proc to expectation block without an argument" do
          # We eval this because Ruby 1.8.6's syntax parser barfs on { |&block| ... }
          # and prevents the entire spec suite from running.
          eval("@double.should_receive(:foo) {|&block| expect(block.call).to eq(:bar)}")
          @double.foo { :bar }
        end

        it "passes proc to expectation block with an argument" do
          eval("@double.should_receive(:foo) {|arg, &block| expect(block.call).to eq(:bar)}")
          @double.foo(:arg) { :bar }
        end

        it "passes proc to stub block without an argurment" do
          eval("@double.stub(:foo) {|&block| expect(block.call).to eq(:bar)}")
          @double.foo { :bar }
        end

        it "passes proc to stub block with an argument" do
          eval("@double.stub(:foo) {|arg, &block| expect(block.call).to eq(:bar)}")
          @double.foo(:arg) { :bar }
        end
      end

      it "fails right away when method defined as never is received" do
        @double.should_receive(:not_expected).never
        expect { @double.not_expected }.
          to raise_error(RSpec::Mocks::MockExpectationError,
                         %Q|(Double "test double").not_expected(no args)\n    expected: 0 times with any arguments\n    received: 1 time|
        )
      end

      it "raises RuntimeError by default" do
        @double.should_receive(:something).and_raise
        expect { @double.something }.to raise_error(RuntimeError)
      end

      it "raises RuntimeError with a message by default" do
        @double.should_receive(:something).and_raise("error message")
        expect { @double.something }.to raise_error(RuntimeError, "error message")
      end

      it "raises an exception of a given type without an error message" do
        @double.should_receive(:something).and_raise(StandardError)
        expect { @double.something }.to raise_error(StandardError)
      end

      it "raises an exception of a given type with a message" do
        @double.should_receive(:something).and_raise(RuntimeError, "error message")
        expect { @double.something }.to raise_error(RuntimeError, "error message")
      end

      it "raises a given instance of an exception" do
        @double.should_receive(:something).and_raise(RuntimeError.new("error message"))
        expect { @double.something }.to raise_error(RuntimeError, "error message")
      end

      class OutOfGas < StandardError
        attr_reader :amount, :units
        def initialize(amount, units)
          @amount = amount
          @units  = units
        end
      end

      it "raises a given instance of an exception with arguments other than the standard 'message'" do
        @double.should_receive(:something).and_raise(OutOfGas.new(2, :oz))

        begin
          @double.something
          fail "OutOfGas was not raised"
        rescue OutOfGas => e
          expect(e.amount).to eq 2
          expect(e.units).to eq :oz
        end
      end

      it "does not raise when told to if args dont match" do
        @double.should_receive(:something).with(2).and_raise(RuntimeError)
        expect {
          @double.something 1
        }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "throws when told to" do
        @double.should_receive(:something).and_throw(:blech)
        expect {
          @double.something
        }.to throw_symbol(:blech)
      end

      it "ignores args on any args" do
        @double.should_receive(:something).at_least(:once).with(any_args)
        @double.something
        @double.something 1
        @double.something "a", 2
        @double.something [], {}, "joe", 7
        verify @double
      end

      it "fails on no args if any args received" do
        @double.should_receive(:something).with(no_args())
        expect {
          @double.something 1
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n  expected: (no args)\n       got: (1)")
      end

      it "fails when args are expected but none are received" do
        @double.should_receive(:something).with(1)
        expect {
          @double.something
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n  expected: (1)\n       got: (no args)")
      end

      it "returns value from block by default" do
        @double.stub(:method_that_yields).and_yield
        value = @double.method_that_yields { :returned_obj }
        expect(value).to eq :returned_obj
        verify @double
      end

      it "yields 0 args to blocks that take a variable number of arguments" do
        @double.should_receive(:yield_back).with(no_args()).once.and_yield
        a = nil
        @double.yield_back {|*x| a = x}
        expect(a).to eq []
        verify @double
      end

      it "yields 0 args multiple times to blocks that take a variable number of arguments" do
        @double.should_receive(:yield_back).once.with(no_args()).once.and_yield.
                                                                    and_yield
        b = []
        @double.yield_back {|*a| b << a}
        expect(b).to eq [ [], [] ]
        verify @double
      end

      it "yields one arg to blocks that take a variable number of arguments" do
        @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
        a = nil
        @double.yield_back {|*x| a = x}
        expect(a).to eq [99]
        verify @double
      end

      it "yields one arg 3 times consecutively to blocks that take a variable number of arguments" do
        @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
                                                                    and_yield(43).
                                                                    and_yield("something fruity")
        b = []
        @double.yield_back {|*a| b << a}
        expect(b).to eq [[99], [43], ["something fruity"]]
        verify @double
      end

      it "yields many args to blocks that take a variable number of arguments" do
        @double.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
        a = nil
        @double.yield_back {|*x| a = x}
        expect(a).to eq [99, 27, "go"]
        verify @double
      end

      it "yields many args 3 times consecutively to blocks that take a variable number of arguments" do
        @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
                                                                    and_yield("wait", :amber).
                                                                    and_yield("stop", 12, :red)
        b = []
        @double.yield_back {|*a| b << a}
        expect(b).to eq [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
        verify @double
      end

      it "yields single value" do
        @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
        a = nil
        @double.yield_back {|x| a = x}
        expect(a).to eq 99
        verify @double
      end

      it "yields single value 3 times consecutively" do
        @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
                                                                    and_yield(43).
                                                                    and_yield("something fruity")
        b = []
        @double.yield_back {|a| b << a}
        expect(b).to eq [99, 43, "something fruity"]
        verify @double
      end

      it "yields two values" do
        @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
        a, b = nil
        @double.yield_back {|x,y| a=x; b=y}
        expect(a).to eq 'wha'
        expect(b).to eq 'zup'
        verify @double
      end

      it "yields two values 3 times consecutively" do
        @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
                                                                    and_yield('not', 'down').
                                                                    and_yield(14, 65)
        c = []
        @double.yield_back {|a,b| c << [a, b]}
        expect(c).to eq [['wha', 'zup'], ['not', 'down'], [14, 65]]
        verify @double
      end

      it "fails when calling yielding method with wrong arity" do
        @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
        expect {
          @double.yield_back {|a|}
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"wha\", \"zup\"| to block with arity of 1")
      end

      it "fails when calling yielding method consecutively with wrong arity" do
        @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
                                                                    and_yield('down').
                                                                    and_yield(14, 65)
        expect {
          c = []
          @double.yield_back {|a,b| c << [a, b]}
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"down\"| to block with arity of 2")
      end

      it "fails when calling yielding method without block" do
        @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
        expect {
          @double.yield_back
        }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" asked to yield |[\"wha\", \"zup\"]| but no block was passed")
      end

      it "is able to double send" do
        @double.should_receive(:send).with(any_args)
        @double.send 'hi'
        verify @double
      end

      it "is able to raise from method calling yielding double" do
        @double.should_receive(:yield_me).and_yield 44

        expect {
          @double.yield_me do |x|
            raise "Bang"
          end
        }.to raise_error(StandardError, "Bang")

        verify @double
      end

      it "clears expectations after verify" do
        @double.should_receive(:foobar)
        @double.foobar
        verify @double
        expect {
          @double.foobar
        }.to raise_error(RSpec::Mocks::MockExpectationError, %q|Double "test double" received unexpected message :foobar with (no args)|)
      end

      it "restores objects to their original state on rspec_reset" do
        double = double("this is a double")
        double.should_receive(:blah)
        reset double
        verify double #should throw if reset didn't work
      end

      it "works even after method_missing starts raising NameErrors instead of NoMethodErrors" do
        # Object#method_missing throws either NameErrors or NoMethodErrors.
        #
        # On a fresh ruby program Object#method_missing:
        #  * raises a NoMethodError when called directly
        #  * raises a NameError when called indirectly
        #
        # Once Object#method_missing has been called at least once (on any object)
        # it starts behaving differently:
        #  * raises a NameError when called directly
        #  * raises a NameError when called indirectly
        #
        # There was a bug in Mock#method_missing that relied on the fact
        # that calling Object#method_missing directly raises a NoMethodError.
        # This example tests that the bug doesn't exist anymore.


        # Ensures that method_missing always raises NameErrors.
        a_method_that_doesnt_exist rescue


        @double.should_receive(:foobar)
        @double.foobar
        verify @double

        expect { @double.foobar }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "temporarily replaces a method stub on a double" do
        @double.stub(:msg).and_return(:stub_value)
        @double.should_receive(:msg).with(:arg).and_return(:double_value)
        expect(@double.msg(:arg)).to equal(:double_value)
        expect(@double.msg).to equal(:stub_value)
        expect(@double.msg).to equal(:stub_value)
        verify @double
      end

      it "does not require a different signature to replace a method stub" do
        @double.stub(:msg).and_return(:stub_value)
        @double.should_receive(:msg).and_return(:double_value)
        expect(@double.msg(:arg)).to equal(:double_value)
        expect(@double.msg).to equal(:stub_value)
        expect(@double.msg).to equal(:stub_value)
        verify @double
      end

      it "raises an error when a previously stubbed method has a negative expectation" do
        @double.stub(:msg).and_return(:stub_value)
        @double.should_not_receive(:msg)
        expect { @double.msg(:arg) }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "temporarily replaces a method stub on a non-double" do
        non_double = Object.new
        non_double.stub(:msg).and_return(:stub_value)
        non_double.should_receive(:msg).with(:arg).and_return(:double_value)
        expect(non_double.msg(:arg)).to equal(:double_value)
        expect(non_double.msg).to equal(:stub_value)
        expect(non_double.msg).to equal(:stub_value)
        verify non_double
      end

      it "returns the stubbed value when no new value specified" do
        @double.stub(:msg).and_return(:stub_value)
        @double.should_receive(:msg)
        expect(@double.msg).to equal(:stub_value)
        verify @double
      end

      it "returns the stubbed value when stubbed with args and no new value specified" do
        @double.stub(:msg).with(:arg).and_return(:stub_value)
        @double.should_receive(:msg).with(:arg)
        expect(@double.msg(:arg)).to equal(:stub_value)
        verify @double
      end

      it "does not mess with the stub's yielded values when also doubleed" do
        @double.stub(:yield_back).and_yield(:stub_value)
        @double.should_receive(:yield_back).and_yield(:double_value)
        @double.yield_back{|v| expect(v).to eq :double_value }
        @double.yield_back{|v| expect(v).to eq :stub_value }
        verify @double
      end

      it "can yield multiple times when told to do so" do
        @double.stub(:foo).and_yield(:stub_value)
        @double.should_receive(:foo).and_yield(:first_yield).and_yield(:second_yield)

        expect { |b| @double.foo(&b) }.to yield_successive_args(:first_yield, :second_yield)
        expect { |b| @double.foo(&b) }.to yield_with_args(:stub_value)

        verify @double
      end

      it "assigns stub return values" do
        double = RSpec::Mocks::Mock.new('name', :message => :response)
        expect(double.message).to eq :response
      end

    end

    describe "a double message receiving a block" do
      before(:each) do
        @double = double("double")
        @calls = 0
      end

      def add_call
        @calls = @calls + 1
      end

      it "calls the block after #should_receive" do
        @double.should_receive(:foo) { add_call }

        @double.foo

        expect(@calls).to eq 1
      end

      it "calls the block after #should_receive after a similar stub" do
        @double.stub(:foo).and_return(:bar)
        @double.should_receive(:foo) { add_call }

        @double.foo

        expect(@calls).to eq 1
      end

      it "calls the block after #once" do
        @double.should_receive(:foo).once { add_call }

        @double.foo

        expect(@calls).to eq 1
      end

      it "calls the block after #twice" do
        @double.should_receive(:foo).twice { add_call }

        @double.foo
        @double.foo

        expect(@calls).to eq 2
      end

      it "calls the block after #times" do
        @double.should_receive(:foo).exactly(10).times { add_call }

        (1..10).each { @double.foo }

        expect(@calls).to eq 10
      end

      it "calls the block after #any_number_of_times" do
        expect(RSpec).to receive(:deprecate).with("any_number_of_times", :replacement => "stub")

        @double.should_receive(:foo).any_number_of_times { add_call }

        (1..7).each { @double.foo }

        expect(@calls).to eq 7
      end

      it "calls the block after #ordered" do
        @double.should_receive(:foo).ordered { add_call }
        @double.should_receive(:bar).ordered { add_call }

        @double.foo
        @double.bar

        expect(@calls).to eq 2
      end
    end

    describe 'string representation generated by #to_s' do
      it 'does not contain < because that might lead to invalid HTML in some situations' do
        double = double("Dog")
        valid_html_str = "#{double}"
        expect(valid_html_str).not_to include('<')
      end
    end

    describe "string representation generated by #to_str" do
      it "looks the same as #to_s" do
        double = double("Foo")
        expect(double.to_str).to eq double.to_s
      end
    end

    describe "double created with no name" do
      it "does not use a name in a failure message" do
        double = double()
        expect {double.foo}.to raise_error(/Double received/)
      end

      it "does respond to initially stubbed methods" do
        double = double(:foo => "woo", :bar => "car")
        expect(double.foo).to eq "woo"
        expect(double.bar).to eq "car"
      end
    end

    describe "==" do
      it "sends '== self' to the comparison object" do
        first = double('first')
        second = double('second')

        first.should_receive(:==).with(second)
        second == first
      end
    end

    describe "with" do
      before { @double = double('double') }
      context "with args" do
        context "with matching args" do
          it "passes" do
            @double.should_receive(:foo).with('bar')
            @double.foo('bar')
          end
        end

        context "with non-matching args" do
          it "fails" do
            @double.should_receive(:foo).with('bar')
            expect do
              @double.foo('baz')
            end.to raise_error
            reset @double
          end
        end

        context "with non-matching doubles" do
          it "fails" do
            d1 = double('1')
            d2 = double('2')
            @double.should_receive(:foo).with(d1)
            expect do
              @double.foo(d2)
            end.to raise_error
            reset @double
          end
        end

        context "with non-matching doubles as_null_object" do
          it "fails" do
            d1 = double('1').as_null_object
            d2 = double('2').as_null_object
            @double.should_receive(:foo).with(d1)
            expect do
              @double.foo(d2)
            end.to raise_error
            reset @double
          end
        end
      end

      context "with a block" do
        context "with matching args" do
          it "returns the result of the block" do
            @double.should_receive(:foo).with('bar') { 'baz' }
            expect(@double.foo('bar')).to eq('baz')
          end
        end

        context "with non-matching args" do
          it "fails" do
            @double.should_receive(:foo).with('bar') { 'baz' }
            expect do
              expect(@double.foo('wrong')).to eq('baz')
            end.to raise_error(/received :foo with unexpected arguments/)
            reset @double
          end
        end
      end
    end
  end
end