File: have_received_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 (727 lines) | stat: -rw-r--r-- 28,440 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
module RSpec
  module Mocks
    # This shared example group is highly unusual as it is used to test how
    # `have_received` works in two situations:
    #
    # * With rspec-mocks as a standalone library.
    # * Together with rspec-expectations.
    #
    # To simulate the former, we've had to hack things a bit. Special care must be taken:
    #
    # * Only define examples with `it`, (not `fit`, `xit`, `specify`, etc). We redefine
    #   `it` below to make it support our needs here but that definition isn't applied to
    #   the other forms.
    # * All normal expectations must use `_expect`, not `expect`. Expectations
    #   for `have_received` should use `expect`.
    RSpec.shared_examples_for Matchers::HaveReceived do
      # Make rspec-expectations' `expect` available via an alias so that when
      # this group is included below in a context that simulates rspec-expectations
      # not being loaded by using rspec-mocks' `expect` instead of rspec-expectations'
      # `expect`, we still have a way to use the expectations one for normal expectations.
      # In this group, all normal expectations should use `_expect` instead of `expect`.
      alias _expect expect

      describe "expect(...).to have_received" do
        it 'passes when the double has received the given message' do
          dbl = double_with_met_expectation(:expected_method)
          expect(dbl).to have_received(:expected_method)
        end

        it 'passes when a null object has received the given message' do
          dbl = null_object_with_met_expectation(:expected_method)
          expect(dbl).to have_received(:expected_method)
        end

        it 'fails when the double has not received the given message' do
          dbl = double_with_unmet_expectation(:expected_method)

          _expect {
            expect(dbl).to have_received(:expected_method)
          }.to raise_error(/expected: 1 time/)
        end

        it "notifies failures via rspec-support's failure notification system" do
          dbl = double_with_unmet_expectation(:expected_method)
          captured = nil

          RSpec::Support.with_failure_notifier(Proc.new { |e, _opt| captured = e }) do
            expect(dbl).to have_received(:expected_method)
          end

          _expect(captured.message).to include("expected: 1 time")
        end

        it 'fails when a null object has not received the given message' do
          dbl = double.as_null_object

          _expect {
            expect(dbl).to have_received(:expected_method)
          }.to raise_error(/expected: 1 time/)
        end

        it 'fails when the method has not been previously stubbed' do
          dbl = double

          _expect {
            expect(dbl).to have_received(:expected_method)
          }.to raise_error(/method has not been stubbed/)
        end

        it 'fails when the method has been mocked' do
          dbl = double
          expect(dbl).to receive(:expected_method)
          dbl.expected_method

          _expect {
            expect(dbl).to have_received(:expected_method)
          }.to raise_error(/method has been mocked instead of stubbed/)
        end

        it "takes a curly-bracket block and yields the arguments given to the stubbed method call" do
          dbl = double(:foo => nil)
          yielded = []
          dbl.foo(:a, :b, :c)
          expect(dbl).to have_received(:foo) { |*args|
            yielded << args
          }
          _expect(yielded).to include([:a, :b, :c])
        end

        it "takes a do-end block and yields the arguments given to the stubbed method call" do
          dbl = double(:foo => nil)
          yielded = []
          dbl.foo(:a, :b, :c)
          expect(dbl).to have_received(:foo) do |*args|
            yielded << args
          end
          _expect(yielded).to include([:a, :b, :c])
        end

        it "passes if expectations against the yielded arguments pass" do
          dbl = double(:foo => nil)
          dbl.foo(42)
          _expect {
            expect(dbl).to have_received(:foo) { |arg|
              _expect(arg).to eq(42)
            }
          }.to_not raise_error
        end

        if RSpec::Support::RubyFeatures.required_kw_args_supported?
          it "passes if expectations against yielded keyword arguments pass" do
            binding.eval(<<-RUBY, __FILE__, __LINE__)
              dbl = double(:foo => nil)
              yielded = []
              dbl.foo(a: 1)
              expect(dbl).to have_received(:foo) do |a:|
                yielded << a
              end
              _expect(yielded).to eq([1])
            RUBY
          end
        end

        it "fails if expectations against the yielded arguments fail" do
          dbl = double(:foo => nil)
          dbl.foo(43)
          _expect {
            expect(dbl).to have_received(:foo) { |arg|
              _expect(arg).to eq(42)
            }
          }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
        end

        it 'gives precedence to a `{ ... }` block when both forms are provided ' \
           'since that form actually binds to `receive`' do
          dbl = double(:foo => nil)
          called = []
          dbl.foo
          expect(dbl).to have_received(:foo) { called << :curly } do
            called << :do_end
          end
          _expect(called).to include(:curly)
          _expect(called).not_to include(:do_end)
        end

        it 'forwards any block passed during method invocation to the `have_received` block' do
          dbl = spy
          block = lambda {}
          dbl.foo(&block)

          expect(dbl).to have_received(:foo) do |&passed_block|
            _expect(passed_block).to be block
          end
        end

        it 'resets expectations on class methods when mocks are reset' do
          dbl = Object
          allow(dbl).to receive(:expected_method)
          dbl.expected_method
          reset dbl
          allow(dbl).to receive(:expected_method)

          _expect {
            expect(dbl).to have_received(:expected_method)
          }.to raise_error(/0 times/)
        end

        context "with" do
          it 'passes when the given args match the args used with the message' do
            dbl = double_with_met_expectation(:expected_method, :expected, :args)
            expect(dbl).to have_received(:expected_method).with(:expected, :args)
          end

          it 'fails when the given args do not match the args used with the message' do
            dbl = double_with_met_expectation(:expected_method, :expected, :args)

            _expect {
              expect(dbl).to have_received(:expected_method).with(:unexpected, :args)
            }.to raise_error(/with unexpected arguments/)
          end
        end

        it 'generates a useful description' do
          matcher = have_received(:expected_method).with(:expected_args).once
          _expect(matcher.description).to eq 'have received expected_method(:expected_args) 1 time'
        end

        it 'can generate a description after mocks have been torn down (e.g. when rspec-core requests it)' do
          matcher = have_received(:expected_method).with(:expected_args).once
          matcher.matches?(double(:expected_method => 1))
          RSpec::Mocks.teardown
          _expect(matcher.description).to eq 'have received expected_method(:expected_args) 1 time'
        end

        it 'produces an error message that matches the expected method if another method was called' do
          my_spy = spy
          my_spy.foo(1)
          my_spy.bar(3)

          _expect {
            expect(my_spy).to have_received(:foo).with(3)
          }.to fail_including("received :foo with unexpected arguments", "expected: (3)", "got: (1)")
        end

        context "counts" do
          let(:the_dbl) { double(:expected_method => nil) }

          before do
            the_dbl.expected_method(:one)
            the_dbl.expected_method(:two)
            the_dbl.expected_method(:one)
          end

          context "when constrained by `with`" do
            it 'only considers the calls with matching args' do
              expect(the_dbl).to have_received(:expected_method).with(:one).twice
              expect(the_dbl).to have_received(:expected_method).with(:two).once
            end

            context "when the message is received without any args matching" do
              it 'includes unmatched args in the error message' do
                _expect {
                  expect(the_dbl).to have_received(:expected_method).with(:three).once
                }.to fail_including("expected: (:three)",
                                    "got:", "(:one) (2 times)",
                                    "(:two) (1 time)")
              end
            end

            context "when the message is received too many times" do
              it 'includes the counts of calls with matching args in the error message' do
                _expect {
                  expect(the_dbl).to have_received(:expected_method).with(:one).once
                }.to fail_including("expected: 1 time", "received: 2 times")
              end
            end

            context "when the message is received too few times" do
              it 'includes the counts of calls with matching args in the error message' do
                _expect {
                  expect(the_dbl).to have_received(:expected_method).with(:two).twice
                }.to fail_including("expected: 2 times", "received: 1 time")
              end
            end

            context "when constrained with grouped arguments `with`" do
              it 'groups the "got" arguments based on the method call that included them' do
                dbl = double(:expected_method => nil)
                dbl.expected_method(:one, :four)
                dbl.expected_method(:two, :four)
                dbl.expected_method(:three, :four)
                dbl.expected_method(:one, :four)
                dbl.expected_method(:three, :four)
                dbl.expected_method(:three, :four)

                _expect {
                  expect(dbl).to have_received(:expected_method).with(:four, :four).once
                }.to fail_including("expected: (:four, :four)",
                                    "got:", "(:one, :four) (2 times)",
                                    "(:two, :four) (1 time)",
                                    "(:three, :four) (3 times)")
              end

              it 'includes single arguments based on the method call that included them' do
                dbl = double(:expected_method => nil)
                dbl.expected_method(:one, :four)

                _expect {
                  expect(dbl).to have_received(:expected_method).with(:three, :four).once
                }.to fail_including("expected: (:three, :four)", "got: (:one, :four)")
              end

              it 'keeps the array combinations distinguished in the group' do
                dbl = double(:expected_method => nil)
                dbl.expected_method([:one], :four)
                dbl.expected_method(:one, [:four])

                _expect {
                  expect(dbl).to have_received(:expected_method).with(:one, :four).once
                }.to fail_including("expected: (:one, :four)",
                                    "got:", "([:one], :four)",
                                    "(:one, [:four])")
              end

              it 'does not group counts on repeated arguments for a single message' do
                dbl = double(:expected_method => nil)
                dbl.expected_method(:one, :one, :two)

                _expect {
                  expect(dbl).to have_received(:expected_method).with(:one, :two, :three).once
                }.to fail_including("expected: (:one, :two, :three)", "got:", "(:one, :one, :two)")
              end
            end
          end

          context "exactly" do
            it 'passes when the message was received the given number of times' do
              expect(the_dbl).to have_received(:expected_method).exactly(3).times
            end

            it 'fails when the message was received more times than expected' do
              _expect {
                expect(the_dbl).to have_received(:expected_method).exactly(1).time
              }.to raise_error(/expected: 1 time.*received: 3 times/m)
              _expect {
                expect(the_dbl).to have_received(:expected_method).exactly(2).times
              }.to raise_error(/expected: 2 times.*received: 3 times/m)
              _expect {
                the_dbl.expected_method
                the_dbl.expected_method
                expect(the_dbl).to have_received(:expected_method).exactly(2).times
              }.to raise_error(/expected: 2 times.*received: 5 times/m)
            end

            it 'fails when the message was received fewer times' do
              _expect {
                expect(the_dbl).to have_received(:expected_method).exactly(4).times
              }.to raise_error(/expected: 4 times.*received: 3 times/m)
            end
          end

          context 'at_least' do
            it 'passes when the message was received the given number of times' do
              expect(the_dbl).to have_received(:expected_method).at_least(3).times
            end

            it 'passes when the message was received more times' do
              expect(the_dbl).to have_received(:expected_method).at_least(2).times
            end

            it 'fails when the message was received fewer times' do
              _expect {
                expect(the_dbl).to have_received(:expected_method).at_least(4).times
              }.to raise_error(/expected: at least 4 times.*received: 3 times/m)
            end
          end

          context 'at_most' do
            it 'passes when the message was received the given number of times' do
              expect(the_dbl).to have_received(:expected_method).at_most(3).times
            end

            it 'passes when the message was received fewer times' do
              expect(the_dbl).to have_received(:expected_method).at_most(4).times
            end

            it 'fails when the message was received more times' do
              _expect {
                expect(the_dbl).to have_received(:expected_method).at_most(2).times
              }.to raise_error(/expected: at most 2 times.*received: 3 times/m)
            end
          end

          context 'once' do
            it 'passes when the message was received once' do
              dbl = double(:expected_method => nil)
              dbl.expected_method
              expect(dbl).to have_received(:expected_method).once
            end

            it 'fails when the message was never received' do
              dbl = double(:expected_method => nil)

              _expect {
                expect(dbl).to have_received(:expected_method).once
              }.to raise_error(/expected: 1 time.*received: 0 times/m)
            end

            it 'fails when the message was received twice' do
              dbl = double(:expected_method => nil)
              dbl.expected_method
              dbl.expected_method

              _expect {
                expect(dbl).to have_received(:expected_method).once
              }.to raise_error(/expected: 1 time.*received: 2 times/m)
            end
          end

          context 'twice' do
            it 'passes when the message was received twice' do
              dbl = double(:expected_method => nil)
              dbl.expected_method
              dbl.expected_method

              expect(dbl).to have_received(:expected_method).twice
            end

            it 'fails when the message was received once' do
              dbl = double(:expected_method => nil)
              dbl.expected_method

              _expect {
                expect(dbl).to have_received(:expected_method).twice
              }.to raise_error(/expected: 2 times.*received: 1 time/m)
            end

            it 'fails when the message was received thrice' do
              dbl = double(:expected_method => nil)
              dbl.expected_method
              dbl.expected_method
              dbl.expected_method

              _expect {
                expect(dbl).to have_received(:expected_method).twice
              }.to raise_error(/expected: 2 times.*received: 3 times/m)
            end
          end

          context 'thrice' do
            it 'passes when the message was received thrice' do
              dbl = double(:expected_method => nil)
              dbl.expected_method
              dbl.expected_method
              dbl.expected_method

              expect(dbl).to have_received(:expected_method).thrice
            end

            it 'fails when the message was received less than three times' do
              dbl = double(:expected_method => nil)
              dbl.expected_method
              dbl.expected_method

              _expect {
                expect(dbl).to have_received(:expected_method).thrice
              }.to raise_error(/expected: 3 times.*received: 2 times/m)
            end

            it 'fails when the message was received more than three times' do
              dbl = double(:expected_method => nil)
              dbl.expected_method
              dbl.expected_method
              dbl.expected_method
              dbl.expected_method

              _expect {
                expect(dbl).to have_received(:expected_method).thrice
              }.to raise_error(/expected: 3 times.*received: 4 times/m)
            end
          end
        end

        context 'ordered' do
          let(:the_dbl) { double :one => 1, :two => 2, :three => 3 }

          it 'passes when the messages were received in order' do
            the_dbl.one
            the_dbl.two

            expect(the_dbl).to have_received(:one).ordered
            expect(the_dbl).to have_received(:two).ordered
          end

          it 'passes with exact receive counts when received in order' do
            3.times { the_dbl.one }
            2.times { the_dbl.two }
            the_dbl.three

            expect(the_dbl).to have_received(:one).thrice.ordered
            expect(the_dbl).to have_received(:two).twice.ordered
            expect(the_dbl).to have_received(:three).once.ordered
          end

          it 'passes with at most receive counts when received in order', :ordered_and_vague_counts_unsupported do
            the_dbl.one
            the_dbl.one
            the_dbl.two

            expect(the_dbl).to have_received(:one).at_most(3).times.ordered
            expect(the_dbl).to have_received(:one).at_most(:thrice).times.ordered
            expect(the_dbl).to have_received(:two).once.ordered
          end

          it 'passes with at least receive counts when received in order', :ordered_and_vague_counts_unsupported do
            the_dbl.one
            the_dbl.one
            the_dbl.two

            expect(the_dbl).to have_received(:one).at_least(1).time.ordered
            expect(the_dbl).to have_received(:two).once.ordered
          end

          it 'fails with exact receive counts when received out of order' do
            the_dbl.one
            the_dbl.two
            the_dbl.one

            _expect {
              expect(the_dbl).to have_received(:one).twice.ordered
              expect(the_dbl).to have_received(:two).once.ordered
            }.to raise_error(/received :two out of order/m)
          end

          it "fails with at most receive counts when received out of order", :ordered_and_vague_counts_unsupported do
            the_dbl.one
            the_dbl.two
            the_dbl.one

            _expect {
              expect(the_dbl).to have_received(:one).at_most(2).times.ordered
              expect(the_dbl).to have_received(:two).once.ordered
            }.to raise_error(/received :two out of order/m)
          end

          it "fails with at least receive counts when received out of order", :ordered_and_vague_counts_unsupported do
            the_dbl.one
            the_dbl.two
            the_dbl.one

            _expect {
              expect(the_dbl).to have_received(:one).at_least(1).time.ordered
              expect(the_dbl).to have_received(:two).once.ordered
            }.to raise_error(/received :two out of order/m)
          end

          it 'fails when the messages are received out of order' do
            the_dbl.two
            the_dbl.one

            _expect {
              expect(the_dbl).to have_received(:one).ordered
              expect(the_dbl).to have_received(:two).ordered
            }.to raise_error(/received :two out of order/m)
          end

          context "when used with `with`" do
            before do
              the_dbl.one(1)
              the_dbl.one(2)
            end

            it "passes when the order lines up" do
              expect(the_dbl).to have_received(:one).with(1).ordered
              expect(the_dbl).to have_received(:one).with(2).ordered
            end

            it "fails when the order is not matched" do
              _expect {
                expect(the_dbl).to have_received(:one).with(2).ordered
                expect(the_dbl).to have_received(:one).with(1).ordered
              }.to fail_with(/received :one out of order/m)
            end
          end

          context "when used on individually allowed messages" do
            before do
              allow(the_dbl).to receive(:foo)
              allow(the_dbl).to receive(:bar)

              the_dbl.foo
              the_dbl.bar
            end

            it 'passes when the messages were received in order' do
              expect(the_dbl).to have_received(:foo).ordered
              expect(the_dbl).to have_received(:bar).ordered
            end

            it 'fails when the messages are received out of order' do
              _expect {
                expect(the_dbl).to have_received(:bar).ordered
                expect(the_dbl).to have_received(:foo).ordered
              }.to raise_error(/received :foo out of order/m)
            end
          end
        end
      end

      describe "expect(...).not_to have_received" do
        it 'passes when the double has not received the given message' do
          dbl = double_with_unmet_expectation(:expected_method)
          expect(dbl).not_to have_received(:expected_method)
        end

        it 'fails when the double has received the given message' do
          dbl = double_with_met_expectation(:expected_method)

          _expect {
            expect(dbl).not_to have_received(:expected_method)
          }.to raise_error(/expected: 0 times.*received: 1 time/m)
        end

        it "notifies failures via rspec-support's failure notification system" do
          dbl = double_with_met_expectation(:expected_method)
          captured = nil

          RSpec::Support.with_failure_notifier(Proc.new { |e, _opt| captured = e }) do
            expect(dbl).not_to have_received(:expected_method)
          end

          _expect(captured.message).to match(/expected: 0 times.*received: 1 time/m)
        end

        it 'fails when the method has not been previously stubbed' do
          dbl = double

          _expect {
            expect(dbl).not_to have_received(:expected_method)
          }.to raise_error(/method has not been stubbed/)
        end

        context "with" do
          it 'passes when the given args do not match the args used with the message' do
            dbl = double_with_met_expectation(:expected_method, :expected, :args)
            expect(dbl).not_to have_received(:expected_method).with(:unexpected, :args)
          end

          it 'fails when the given args match the args used with the message' do
            dbl = double_with_met_expectation(:expected_method, :expected, :args)

            _expect {
              expect(dbl).not_to have_received(:expected_method).with(:expected, :args)
            }.to raise_error(/expected: 0 times.*received: 1 time/m) # TODO: better message
          end
        end

        %w[exactly at_least at_most times once twice].each do |constraint|
          it "does not allow #{constraint} to be used because it creates confusion" do
            dbl = double_with_unmet_expectation(:expected_method)
            _expect {
              expect(dbl).not_to have_received(:expected_method).send(constraint)
            }.to raise_error(/can't use #{constraint} when negative/)
          end
        end
      end

      describe "allow(...).to have_received" do
        it "fails because it's nonsensical" do
          _expect {
            allow(double).to have_received(:some_method)
          }.to fail_with("Using allow(...) with the `have_received` matcher is not supported as it would have no effect.")
        end
      end

      describe "allow_any_instance_of(...).to have_received" do
        it "fails because it's nonsensical" do
          _expect {
            allow_any_instance_of(double).to have_received(:some_method)
          }.to fail_with("Using allow_any_instance_of(...) with the `have_received` matcher is not supported.")
        end
      end

      describe "expect_any_instance_of(...).to have_received" do
        it "fails because we dont want to support it" do
          _expect {
            expect_any_instance_of(double).to have_received(:some_method)
          }.to fail_with("Using expect_any_instance_of(...) with the `have_received` matcher is not supported.")
        end
      end

      describe "expect_any_instance_of(...).not_to have_received" do
        it "fails because we dont want to support it" do
          _expect {
            expect_any_instance_of(double).not_to have_received(:some_method)
          }.to fail_with("Using expect_any_instance_of(...) with the `have_received` matcher is not supported.")
        end
      end

      def double_with_met_expectation(method_name, *args)
        double = double_with_unmet_expectation(method_name)
        meet_expectation(double, method_name, *args)
      end

      def null_object_with_met_expectation(method_name, *args)
        meet_expectation(double.as_null_object, method_name, *args)
      end

      def meet_expectation(double, method_name, *args)
        double.send(method_name, *args)
        double
      end

      def double_with_unmet_expectation(method_name)
        double('double', method_name => true)
      end
    end

    RSpec.describe Matchers::HaveReceived, "when used in a context that has rspec-mocks and rspec-expectations available" do
      include_examples Matchers::HaveReceived do
        # Override `fail_including` for this context, since `have_received` is a normal
        # rspec-expectations matcher, the error class is different.
        def fail_including(*snippets)
          raise_error(RSpec::Expectations::ExpectationNotMetError, a_string_including(*snippets))
        end
      end
    end

    RSpec.describe Matchers::HaveReceived, "when used in a context that has only rspec-mocks available" do
      # We use a delegator here so that matchers can still be created
      # via the `RSpec::Matchers` methods. This works because we
      # instantiate `MocksScope` with the example group instance, so
      # all undefined methods (including matcher methods) forward to it.
      # However, `RSpec::Mocks::ExampleMethods` defines `expect` so instances
      # of this class use the version of `expect` defined in rspec-mocks, not
      # the one from rspec-expectations.
      class MocksScope
        include RSpec::Mocks::ExampleMethods

        def initialize(example_group)
          @example_group = example_group
        end

        def method_missing(*args, &block)
          @example_group.__send__(*args, &block)
        end
      end

      # Redefine `it` so that we can eval each example in a special scope
      # to simulate rspec-expectations not being loaded.
      def self.it(*args, &block)
        # Necessary so line-number filtering works...
        args << {} unless Hash === args.last
        args.last[:caller] = caller

        # delegate to the normal `it`...
        super(*args) do
          # ...but eval the block in a special scope that has `expect`
          # from rspec-mocks, not from rspec-expectations.
          MocksScope.new(self).instance_exec(&block)
        end
      end

      include_examples Matchers::HaveReceived
    end
  end
end