File: receive_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (766 lines) | stat: -rw-r--r-- 26,865 bytes parent folder | download
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
module RSpec
  module Mocks
    RSpec.describe Matchers::Receive do
      include_context "with syntax", :expect

      describe "expectations/allowances on any instance recorders" do
        include_context "with syntax", [:expect, :should]

        it "warns about allow(Klass.any_instance).to receive..." do
          expect(RSpec).to receive(:warning).with(/allow.*any_instance.*is probably not what you meant.*allow_any_instance_of.*instead/)
          allow(Object.any_instance).to receive(:foo)
        end

        it "includes the correct call site in the allow warning" do
          expect_warning_with_call_site(__FILE__, __LINE__ + 1)
          allow(Object.any_instance).to receive(:foo)
        end

        it "warns about expect(Klass.any_instance).to receive..." do
          expect(RSpec).to receive(:warning).with(/expect.*any_instance.*is probably not what you meant.*expect_any_instance_of.*instead/)
          any_instance_proxy = Object.any_instance
          expect(any_instance_proxy).to receive(:foo)
          any_instance_proxy.foo
        end

        it "includes the correct call site in the expect warning" do
          any_instance_proxy = Object.any_instance
          expect_warning_with_call_site(__FILE__, __LINE__ + 1)
          expect(any_instance_proxy).to receive(:foo)
          any_instance_proxy.foo
        end
      end

      # FIXME: this is defined here to prevent
      # "warning: method redefined; discarding old kw_args_method"
      # because shared examples are evaluated several times.
      # When we flatten those shared examples in RSpec 4 because
      # of no "should" syntax, it will become possible to put this
      # class definition closer to examples that use it.
      if RSpec::Support::RubyFeatures.required_kw_args_supported?
        binding.eval(<<-RUBY, __FILE__, __LINE__)
        class TestObject
          def kw_args_method(a:, b:); end
        end
        RUBY
      end

      shared_examples "a receive matcher" do |*options|
        it 'allows the caller to configure how the subject responds' do
          wrapped.to receive(:foo).and_return(5)
          expect(receiver.foo).to eq(5)
        end

        it 'allows the caller to constrain the received arguments' do
          wrapped.to receive(:foo).with(:a)
          receiver.foo(:a)

          expect {
            receiver.foo(:b)
          }.to raise_error(/received :foo with unexpected arguments/)
        end

        it 'allows the caller to constrain the received arguments by matcher' do
          wrapped.to receive(:foo).with an_instance_of Float
          expect {
            receiver.foo(1)
          }.to raise_error(/expected.*\(an instance of Float\)/)
          receiver.foo(1.1)
        end

        context 'without yielding receiver' do
          # when `yield_receiver_to_any_instance_implementation_blocks` is `true`
          # the block arguments are different for `expect` and `expect_any_instance_of`
          around do |example|
             previous_value = RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks?
             RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = false
             example.run
             RSpec::Mocks.configuration.yield_receiver_to_any_instance_implementation_blocks = previous_value
          end

          it 'allows a `do...end` block implementation to be provided' do
            wrapped.to receive(:foo) do
              4
            end

            expect(receiver.foo).to eq(4)
          end

          if RSpec::Support::RubyFeatures.kw_args_supported?
            binding.eval(<<-RUBY, __FILE__, __LINE__)
            it 'allows a `do...end` block implementation with keyword args to be provided' do
              wrapped.to receive(:foo) do |**kwargs|
                kwargs[:kw]
              end

              expect(receiver.foo(kw: :arg)).to eq(:arg)
            end

            it 'allows a `do...end` block implementation with optional keyword args to be provided' do
              wrapped.to receive(:foo) do |kw: :arg|
                kw
              end

              expect(receiver.foo(kw: 1)).to eq(1)
            end

            it 'allows a `do...end` block implementation with optional keyword args to be provided' do
              wrapped.to receive(:foo) do |kw: :arg|
                kw
              end

              expect(receiver.foo).to eq(:arg)
            end
            RUBY
          end

          if RSpec::Support::RubyFeatures.required_kw_args_supported?
            binding.eval(<<-RUBY, __FILE__, __LINE__)
            it 'allows a `do...end` block implementation with required keyword args' do
              wrapped.to receive(:foo) do |kw:|
                kw
              end

              expect(receiver.foo(kw: :arg)).to eq(:arg)
            end

            it "expects to receive keyword args" do
              dbl = instance_double(TestObject)
              expect(dbl).to receive(:kw_args_method).with(a: 1, b: 2)
              dbl.kw_args_method(a: 1, b: 2)
            end

            if RUBY_VERSION >= '3.0'
              it "fails to expect to receive hash with keyword args" do
                expect {
                  dbl = instance_double(TestObject)
                  expect(dbl).to receive(:kw_args_method).with(a: 1, b: 2)
                  dbl.kw_args_method({a: 1, b: 2})
                }.to fail_with do |failure|
                  reset_all
                  expect(failure.message)
                    .to include('expected: ({:a=>1, :b=>2}) (keyword arguments)')
                    .and include('got: ({:a=>1, :b=>2}) (options hash)')
                end
              end
            else
              it "expects to receive hash with keyword args" do
                dbl = instance_double(TestObject)
                expect(dbl).to receive(:kw_args_method).with(a: 1, b: 2)
                dbl.kw_args_method({a: 1, b: 2})
              end
            end

            it "expects to receive hash with a hash" do
              dbl = instance_double(TestObject)
              expect(dbl).to receive(:kw_args_method).with({a: 1, b: 2})
              dbl.kw_args_method({a: 1, b: 2})
            end

            it "expects to receive keyword args with a hash" do
              dbl = instance_double(TestObject)
              expect(dbl).to receive(:kw_args_method).with({a: 1, b: 2})
              dbl.kw_args_method(a: 1, b: 2)
            end
            RUBY
          end
        end

        it 'allows chaining off a `do...end` block implementation to be provided' do
          wrapped.to receive(:foo) do
            4
          end.and_return(6)

          expect(receiver.foo).to eq(6)
        end

        it 'allows a `{ ... }` block implementation to be provided' do
          wrapped.to receive(:foo) { 5 }
          expect(receiver.foo).to eq(5)
        end

        it 'gives precedence to a `{ ... }` block when both forms are provided ' \
           'since that form actually binds to `receive`' do
          wrapped.to receive(:foo) { :curly } do
            :do_end
          end

          expect(receiver.foo).to eq(:curly)
        end

        it 'does not support other matchers', :unless => options.include?(:allow_other_matchers) do
          expect {
            wrapped.to eq(3)
          }.to raise_error(UnsupportedMatcherError)
        end

        it 'does support inherited matchers', :unless => options.include?(:allow_other_matchers) do
          receive_foo = Class.new(RSpec::Mocks::Matchers::Receive).new(:foo, nil)
          wrapped.to receive_foo
          receiver.foo
        end

        it 'does not get confused by messages being passed as strings and symbols' do
          wrapped.to receive(:foo).with(1) { :a }
          wrapped.to receive("foo").with(2) { :b }

          expect(receiver.foo(1)).to eq(:a)
          expect(receiver.foo(2)).to eq(:b)
        end

        it 'allows do...end blocks to be passed to the fluent interface methods without getting a warning' do
          expect(RSpec).not_to receive(:warning)

          wrapped.to receive(:foo).with(1) do
            :a
          end

          expect(receiver.foo(1)).to eq(:a)
        end

        it 'makes { } blocks trump do...end blocks when passed to a fluent interface method' do
          wrapped.to receive(:foo).with(1) { :curly } do
            :do_end
          end

          expect(receiver.foo(1)).to eq(:curly)
        end
      end

      shared_examples "an expect syntax allowance" do |*options|
        it_behaves_like "a receive matcher", *options

        it 'does not expect the message to be received' do
          wrapped.to receive(:foo)
          expect { verify_all }.not_to raise_error
        end
      end

      shared_examples "an expect syntax negative allowance" do
        it 'is disabled since this expression is confusing' do
          expect {
            wrapped.not_to receive(:foo)
          }.to raise_error(/not_to receive` is not supported/)

          expect {
            wrapped.to_not receive(:foo)
          }.to raise_error(/to_not receive` is not supported/)
        end
      end

      shared_examples "an expect syntax expectation" do |*options|
        it_behaves_like "a receive matcher", *options

        it 'sets up a message expectation that passes if the message is received' do
          wrapped.to receive(:foo)
          receiver.foo
          verify_all
        end

        it 'sets up a message expectation that fails if the message is not received' do
          wrapped.to receive(:foo)

          expect {
            verify_all
          }.to fail
        end

        it "reports the line number of expectation of unreceived message", :pending => options.include?(:does_not_report_line_num) do
          expected_error_line = __LINE__; wrapped.to receive(:foo)

          expect {
            verify_all
          }.to raise_error { |e|
            expect(e.backtrace.first).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
          }
        end

        it "provides a useful matcher description" do
          matcher = receive(:foo).with(:bar).once
          wrapped.to matcher
          receiver.foo(:bar)

          expect(matcher.description).to start_with("receive foo")
        end
      end

      shared_examples "an expect syntax negative expectation" do
        it 'sets up a negative message expectation that passes if the message is not received' do
          wrapped.not_to receive(:foo)
          verify_all
        end

        it 'sets up a negative message expectation that fails if the message is received' do
          wrapped.not_to receive(:foo)

          expect_fast_failure_from(receiver, /expected: 0 times.*received: 1 time/m) do
            receiver.foo
          end
        end

        it 'supports `to_not` as an alias for `not_to`' do
          wrapped.to_not receive(:foo)

          expect_fast_failure_from(receiver, /expected: 0 times.*received: 1 time/m) do
            receiver.foo
          end
        end

        it 'allows the caller to constrain the received arguments' do
          wrapped.not_to receive(:foo).with(:a)
          def receiver.method_missing(*); end # a poor man's stub...

          expect {
            receiver.foo(:b)
          }.not_to raise_error

          expect_fast_failure_from(receiver, /expected: 0 times.*received: 1 time/m) do
            receiver.foo(:a)
          end
        end

        it 'prevents confusing double-negative expressions involving `never`' do
          expect {
            wrapped.not_to receive(:foo).never
          }.to raise_error(/trying to negate it again/)

          expect {
            wrapped.to_not receive(:foo).never
          }.to raise_error(/trying to negate it again/)
        end
      end

      shared_examples "resets partial mocks cleanly" do
        let(:klass)  { Struct.new(:foo) }
        let(:object) { klass.new :bar }

        it "removes the method double" do
          target.to receive(:foo).and_return(:baz)
          expect { reset object }.to change { object.foo }.from(:baz).to(:bar)
        end
      end

      shared_examples "resets partial mocks of any instance cleanly" do
        let(:klass)  { Struct.new(:foo) }
        let(:object) { klass.new :bar }

        it "removes the method double" do
          target.to receive(:foo).and_return(:baz)
          expect {
            verify_all
          }.to change { object.foo }.from(:baz).to(:bar)
        end
      end

      shared_examples "handles frozen objects cleanly" do
        let(:klass)  { Struct.new(:foo) }
        let(:object) { klass.new :bar }

        context "when adding the method double" do
          it "raises clear error" do
            object.freeze
            expect {
              target.to receive(:foo).and_return(:baz)
            }.to raise_error(ArgumentError, /Cannot proxy frozen objects/)
          end
        end

        context "when removing the method double" do
          before do
            if RUBY_VERSION < "2.2" && RUBY_VERSION >= "2.0" && RSpec::Support::Ruby.mri?
              pending "Does not work on 2.0-2.1 because frozen structs can have methods restored"
            end
          end

          it "warns about being unable to remove the method double" do
            target.to receive(:foo).and_return(:baz)
            expect_warning_without_call_site(/rspec-mocks was unable to restore the original `foo` method on #{object.inspect}/)
            object.freeze
            reset object
          end

          it "includes the spec location in the warning" do
            line = __LINE__ - 1
            target.to receive(:foo).and_return(:baz)
            expect_warning_without_call_site(/#{RSpec::Core::Metadata.relative_path(__FILE__)}:#{line}/)
            object.freeze
            reset object
          end
        end

        context "with fake frozen object" do
          let(:klass)  { Struct.new(:foo, :frozen?, :freeze) }
          let(:object) { klass.new :bar, true }

          it "allows the caller to configure how the subject responds" do
            object.freeze
            target.to receive(:foo).and_return(5)
            expect(object.foo).to eq(5)
            expect { reset object }.to change { object.foo }.from(5).to(:bar)
          end
        end
      end

      describe "allow(...).to receive" do
        it_behaves_like "an expect syntax allowance" do
          let(:receiver) { double }
          let(:wrapped)  { allow(receiver) }
        end
        it_behaves_like "resets partial mocks cleanly" do
          let(:target) { allow(object) }
        end
        it_behaves_like "handles frozen objects cleanly" do
          let(:target) { allow(object) }
        end

        context 'ordered with receive counts' do
          specify 'is not supported' do
            a_dbl = double
            expect_warning_with_call_site(__FILE__, __LINE__ + 1)
            allow(a_dbl).to receive(:one).ordered
          end
        end

        context 'on a class method, from a class with subclasses' do
          let(:superclass)     { Class.new { def self.foo; "foo"; end }}
          let(:subclass_redef) { Class.new(superclass) { def self.foo; ".foo."; end }}
          let(:subclass_deleg) { Class.new(superclass) { def self.foo; super.upcase; end }}
          let(:subclass_asis)  { Class.new(superclass) }

          context 'if the method is redefined in the subclass' do
            it 'does not stub the method in the subclass' do
              allow(superclass).to receive(:foo) { "foo!!" }
              expect(superclass.foo).to eq "foo!!"
              expect(subclass_redef.foo).to eq ".foo."
            end
          end

          context 'if the method is not redefined in the subclass' do
            it 'stubs the method in the subclass' do
              allow(superclass).to receive(:foo) { "foo!!" }
              expect(superclass.foo).to eq "foo!!"
              expect(subclass_asis.foo).to eq "foo!!"
            end
          end

          it 'creates stub which can be called using `super` in a subclass' do
            allow(superclass).to receive(:foo) { "foo!!" }
            expect(subclass_deleg.foo).to eq "FOO!!"
          end

          it 'can stub the same method simultaneously in the superclass and subclasses' do
            allow(subclass_redef).to receive(:foo) { "__foo__" }
            allow(superclass).to     receive(:foo) { "foo!!" }
            allow(subclass_deleg).to receive(:foo) { "$$foo$$" }

            expect(subclass_redef.foo).to eq "__foo__"
            expect(superclass.foo).to     eq "foo!!"
            expect(subclass_deleg.foo).to eq "$$foo$$"
          end
        end
      end

      describe "allow(...).not_to receive" do
        it_behaves_like "an expect syntax negative allowance" do
          let(:wrapped) { allow(double) }
        end
      end

      describe "allow_any_instance_of(...).to receive" do
        it_behaves_like "an expect syntax allowance" do
          let(:klass)    { Class.new }
          let(:wrapped)  { allow_any_instance_of(klass) }
          let(:receiver) { klass.new }
        end

        it_behaves_like "resets partial mocks of any instance cleanly" do
          let(:target) { allow_any_instance_of(klass) }
        end
      end

      describe "allow_any_instance_of(...).not_to receive" do
        it_behaves_like "an expect syntax negative allowance" do
          let(:wrapped) { allow_any_instance_of(Class.new) }
        end
      end

      describe "expect(...).to receive" do
        it_behaves_like "an expect syntax expectation", :allow_other_matchers do
          let(:receiver) { double }
          let(:wrapped)  { expect(receiver) }

          context "when a message is not received" do
            it 'sets up a message expectation that formats argument matchers correctly' do
              wrapped.to receive(:foo).with an_instance_of Float
              expect { verify_all }.to(
                raise_error(/expected: 1 time with arguments: \(an instance of Float\)\n\s+received: 0 times$/)
              )
            end
          end

          context "when a message is received the wrong number of times" do
            it "sets up a message expectation that formats argument matchers correctly" do
              wrapped.to receive(:foo).with(anything, hash_including(:bar => anything))

              receiver.foo(1, :bar => 2)
              receiver.foo(1, :bar => 3)

              expect { verify_all }.to(
                raise_error(/received: 2 times with arguments: \(anything, hash_including\(:bar=>"anything"\)\)$/)
              )
            end
          end
        end
        it_behaves_like "resets partial mocks cleanly" do
          let(:target) { expect(object) }
        end
        it_behaves_like "handles frozen objects cleanly" do
          let(:target) { expect(object) }
        end

        context "ordered with receive counts" do
          let(:dbl) { double(:one => 1, :two => 2) }

          it "passes with exact receive counts when the ordering is correct" do
            expect(dbl).to receive(:one).twice.ordered
            expect(dbl).to receive(:two).once.ordered

            dbl.one
            dbl.one
            dbl.two
          end

          it "fails with exact receive counts when the ordering is incorrect" do
            expect {
              expect(dbl).to receive(:one).twice.ordered
              expect(dbl).to receive(:two).once.ordered

              dbl.one
              dbl.two
              dbl.one
            }.to raise_error(/out of order/)

            reset_all
          end

          it "passes with at least when the ordering is correct" do
            expect(dbl).to receive(:one).at_least(2).times.ordered
            expect(dbl).to receive(:two).once.ordered

            dbl.one
            dbl.one
            dbl.one
            dbl.two
          end

          it "fails with at least when the ordering is incorrect", :ordered_and_vague_counts_unsupported do
            expect {
              expect(dbl).to receive(:one).at_least(2).times.ordered
              expect(dbl).to receive(:two).once.ordered

              dbl.one
              dbl.two
            }.to fail

            reset_all
          end

          it "passes with at most when the ordering is correct" do
            expect(dbl).to receive(:one).at_most(2).times.ordered
            expect(dbl).to receive(:two).once.ordered

            dbl.one
            dbl.two
          end

          it "fails with at most when the ordering is incorrect", :ordered_and_vague_counts_unsupported do
            expect {
              expect(dbl).to receive(:one).at_most(2).times.ordered
              expect(dbl).to receive(:two).once.ordered

              dbl.one
              dbl.one
              dbl.one
              dbl.two
            }.to fail

            reset_all
          end

          it 'does not result in infinite recursion when `respond_to?` is stubbed' do
            # Setting a method expectation causes the method to be proxied
            # RSpec may call #respond_to? when processing a failed expectation
            # If those internal calls go to the proxied method, that could
            #   result in another failed expectation error, causing infinite loop
            expect {
              obj = Object.new
              expect(obj).to receive(:respond_to?).with('something highly unlikely')
              obj.respond_to?(:not_what_we_wanted)
            }.to raise_error(/received :respond_to\? with unexpected arguments/)
            reset_all
          end
        end
      end

      describe "expect_any_instance_of(...).to receive" do
        it_behaves_like "an expect syntax expectation", :does_not_report_line_num do
          let(:klass)    { Class.new }
          let(:wrapped)  { expect_any_instance_of(klass) }
          let(:receiver) { klass.new }

          it 'sets up a message expectation that formats argument matchers correctly' do
            wrapped.to receive(:foo).with an_instance_of Float
            expect { verify_all }.to raise_error(/should have received the following message\(s\) but didn't/)
          end
        end
        it_behaves_like "resets partial mocks of any instance cleanly" do
          let(:target) { expect_any_instance_of(klass) }
        end
      end

      describe "expect(...).not_to receive" do
        it_behaves_like "an expect syntax negative expectation" do
          let(:receiver) { double }
          let(:wrapped)  { expect(receiver) }
        end
      end

      describe "expect_any_instance_of(...).not_to receive" do
        it_behaves_like "an expect syntax negative expectation" do
          let(:klass)    { Class.new }
          let(:wrapped)  { expect_any_instance_of(klass) }
          let(:receiver) { klass.new }
        end
      end

      it 'has a description before being matched' do
        matcher = receive(:foo)
        expect(matcher.description).to eq("receive foo")
      end

      shared_examples "using rspec-mocks in another test framework" do
        it 'can use the `expect` syntax' do
          dbl = double

          framework.new.instance_exec do
            expect(dbl).to receive(:foo).and_return(3)
          end

          expect(dbl.foo).to eq(3)
        end

        it 'expects the method to be called when `expect` is used' do
          dbl = double

          framework.new.instance_exec do
            expect(dbl).to receive(:foo)
          end

          expect { verify dbl }.to fail
        end

        it 'supports `expect(...).not_to receive`' do
          expect_fast_failure_from(double) do |dbl|
            framework.new.instance_exec do
              expect(dbl).not_to receive(:foo)
            end

            dbl.foo
          end
        end

        it 'supports `expect(...).to_not receive`' do
          expect_fast_failure_from(double) do |dbl|
            framework.new.instance_exec do
              expect(dbl).to_not receive(:foo)
            end

            dbl.foo
          end
        end
      end

      context "when used in a test framework without rspec-expectations" do
        let(:framework) do
          Class.new do
            include RSpec::Mocks::ExampleMethods

            def eq(_)
              double("MyMatcher")
            end
          end
        end

        it_behaves_like "using rspec-mocks in another test framework"

        it 'cannot use `expect` with another matcher' do
          expect {
            framework.new.instance_exec do
              expect(3).to eq(3)
            end
          }.to raise_error(/only the `receive`, `have_received` and `receive_messages` matchers are supported with `expect\(...\).to`/)
        end

        it 'can toggle the available syntax' do
          expect(framework.new).to respond_to(:expect)
          RSpec::Mocks.configuration.syntax = :should
          expect(framework.new).not_to respond_to(:expect)
          RSpec::Mocks.configuration.syntax = :expect
          expect(framework.new).to respond_to(:expect)
        end

        after { RSpec::Mocks.configuration.syntax = :expect }
      end

      context "when rspec-expectations is included in the test framework first" do
        before do
          # the examples here assume `expect` is define in RSpec::Matchers...
          expect(RSpec::Matchers.method_defined?(:expect)).to be_truthy
        end

        let(:framework) do
          Class.new do
            include RSpec::Matchers
            include RSpec::Mocks::ExampleMethods
          end
        end

        it_behaves_like "using rspec-mocks in another test framework"

        it 'can use `expect` with any matcher' do
          framework.new.instance_exec do
            expect(3).to eq(3)
          end
        end

        it 'with a nonsense allowance it fails with a reasonable error message' do
          expect {
            allow(true).not_to be_nil
          }.to raise_error(start_with("`allow(...).not_to be_nil` is not supported since it doesn't really make sense"))
        end
      end

      context "when rspec-expectations is included in the test framework last" do
        before do
          # the examples here assume `expect` is define in RSpec::Matchers...
          expect(RSpec::Matchers.method_defined?(:expect)).to be_truthy
        end

        let(:framework) do
          Class.new do
            include RSpec::Mocks::ExampleMethods
            include RSpec::Matchers
          end
        end

        it_behaves_like "using rspec-mocks in another test framework"

        it 'can use `expect` with any matcher' do
          framework.new.instance_exec do
            expect(3).to eq(3)
          end
        end
      end
    end
  end
end