File: failure_notification_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 (76 lines) | stat: -rw-r--r-- 2,574 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
RSpec.describe "Failure notification" do
  def capture_errors(&block)
    errors = []
    RSpec::Support.with_failure_notifier(Proc.new { |e, _opts| errors << e }, &block)
    errors
  end

  it "uses the rspec-support notifier to support `aggregate_failures`" do
    dbl = double("Foo")

    expect(capture_errors { dbl.some_unallowed_method }).to match [an_object_having_attributes(
      :message => a_string_including(dbl.inspect, "some_unallowed_method")
    )]
  end

  it "includes the line of future expectation in the notification for an unreceived message" do
    dbl = double("Foo")
    expect(dbl).to receive(:wont_happen); expected_from_line = __LINE__

    error = capture_errors { verify dbl }.first
    expect(error.backtrace.first).to match(/#{File.basename(__FILE__)}:#{expected_from_line}/)
  end

  it "does not allow a double to miscount the number of times a message was received when a failure is notified in an alternate way" do
    dbl = double("Foo")
    expect(dbl).not_to receive(:bar)

    capture_errors { dbl.bar }

    expect { verify dbl }.to fail_including("expected: 0 times", "received: 1 time")
  end

  context "when using `aggregate_failures`" do
    specify 'spy failures for unreceived messages are reported correctly' do
      expect {
        aggregate_failures do
          expect(spy).to have_received(:foo)
        end
      }.to raise_error(RSpec::Expectations::ExpectationNotMetError) do |e|
        expect(e).not_to be_a(RSpec::Expectations::MultipleExpectationsNotMetError)
        expect(e.message).to include("expected: 1 time", "received: 0 times")
      end
    end

    specify 'spy failures for messages received with unexpected args are reported correctly' do
      expect {
        aggregate_failures do
          the_spy = spy
          the_spy.foo(1)
          expect(the_spy).to have_received(:foo).with(2)
        end
      }.to raise_error(RSpec::Expectations::ExpectationNotMetError) do |e|
        expect(e).not_to be_a(RSpec::Expectations::MultipleExpectationsNotMetError)
        expect(e.message).to include("expected: (2)", "got: (1)")
      end
    end

    specify "failing negative expectations are only notified once" do
      expect {
        aggregate_failures do
          dbl = double

          expect(dbl).not_to receive(:foo)
          expect(dbl).not_to receive(:bar)

          dbl.foo
          dbl.bar

          verify_all
        end
      }.to raise_error(RSpec::Expectations::MultipleExpectationsNotMetError) do |e|
        expect(e.failures.count).to eq(2)
      end
    end
  end
end