Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 6687069

Browse files
committed
Fix expect_warn_deprecation matching any message
expect_warn_deprecation matches any message when there's raise_error(RSpec::Expectations::ExpectationNotMetError) in the example, e.g.: it 'prints a deprecation warning when given a value and negated' do expect_warn_deprecation(/complete nonsense/) expect { expect(3).not_to matcher }.to fail end
1 parent 7774664 commit 6687069

File tree

2 files changed

+118
-29
lines changed

2 files changed

+118
-29
lines changed

lib/rspec/support/spec/deprecation_helpers.rb

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
11
module RSpecHelpers
2-
def expect_no_deprecation
3-
expect(RSpec.configuration.reporter).not_to receive(:deprecation)
4-
end
5-
62
def expect_deprecation_with_call_site(file, line, snippet=//)
7-
expect(RSpec.configuration.reporter).to receive(:deprecation) do |options|
8-
expect(options[:call_site]).to include([file, line].join(':'))
9-
expect(options[:deprecated]).to match(snippet)
10-
end
3+
expect(RSpec.configuration.reporter).to receive(:deprecation).
4+
with(include(:deprecated => match(snippet), :call_site => include([file, line].join(':'))))
115
end
126

137
def expect_deprecation_without_call_site(snippet=//)
14-
expect(RSpec.configuration.reporter).to receive(:deprecation) do |options|
15-
expect(options[:call_site]).to eq nil
16-
expect(options[:deprecated]).to match(snippet)
17-
end
8+
expect(RSpec.configuration.reporter).to receive(:deprecation).
9+
with(include(:deprecated => match(snippet), :call_site => eq(nil)))
1810
end
1911

2012
def expect_warn_deprecation_with_call_site(file, line, snippet=//)
21-
expect(RSpec.configuration.reporter).to receive(:deprecation) do |options|
22-
message = options[:message]
23-
expect(message).to match(snippet)
24-
expect(message).to include([file, line].join(':'))
25-
end
13+
expect(RSpec.configuration.reporter).to receive(:deprecation).
14+
with(include(:message => match(snippet), :call_site => include([file, line].join(':'))))
2615
end
2716

2817
def expect_warn_deprecation(snippet=//)
29-
expect(RSpec.configuration.reporter).to receive(:deprecation) do |options|
30-
message = options[:message]
31-
expect(message).to match(snippet)
32-
end
18+
expect(RSpec.configuration.reporter).to receive(:deprecation).
19+
with(include(:message => match(snippet)))
3320
end
3421

3522
def allow_deprecation
@@ -39,19 +26,16 @@ def allow_deprecation
3926
def expect_no_deprecations
4027
expect(RSpec.configuration.reporter).not_to receive(:deprecation)
4128
end
29+
alias expect_no_deprecation expect_no_deprecations
4230

4331
def expect_warning_without_call_site(expected=//)
44-
expect(::Kernel).to receive(:warn) do |message|
45-
expect(message).to match expected
46-
expect(message).to_not match(/Called from/)
47-
end
32+
expect(::Kernel).to receive(:warn).
33+
with(match(expected).and(satisfy { |message| !message.match?(/Called from/) }))
4834
end
4935

5036
def expect_warning_with_call_site(file, line, expected=//)
51-
expect(::Kernel).to receive(:warn) do |message|
52-
expect(message).to match expected
53-
expect(message).to match(/Called from #{file}:#{line}/)
54-
end
37+
expect(::Kernel).to receive(:warn).
38+
with(match(expected).and(match(/Called from #{file}:#{line}/)))
5539
end
5640

5741
def expect_no_warnings
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
require 'rspec/matchers/fail_matchers'
2+
3+
RSpec.describe RSpecHelpers do
4+
# include RSpec::Matchers::FailMatchers
5+
6+
def deprecate!(message)
7+
RSpec.configuration.reporter.deprecation(:message => message)
8+
end
9+
10+
def fail_with(snippet)
11+
raise_error(RSpec::Mocks::MockExpectationError, snippet)
12+
end
13+
14+
def raise_unrelated_expectation!
15+
raise(RSpec::Expectations::ExpectationNotMetError, 'abracadabra')
16+
end
17+
18+
describe '#expect_no_deprecations' do
19+
shared_examples_for 'expects no deprecations' do
20+
it 'passes when there were no deprecations' do
21+
expectation
22+
end
23+
24+
it 'fails when there was a deprecation warning' do
25+
in_sub_process do
26+
expect {
27+
expectation
28+
deprecate!('foo')
29+
}.to fail_with(/received: 1 time/)
30+
end
31+
end
32+
33+
it 'fails with a MockExpectationError when there was also an ExpectationNotMetError' do
34+
in_sub_process do
35+
expect {
36+
expectation
37+
deprecate!('bar')
38+
raise_unrelated_expectation!
39+
}.to fail_with(/received: 1 time/)
40+
end
41+
end
42+
end
43+
44+
it_behaves_like 'expects no deprecations' do
45+
def expectation
46+
expect_no_deprecations
47+
end
48+
end
49+
50+
# Alias
51+
it_behaves_like 'expects no deprecations' do
52+
def expectation
53+
expect_no_deprecation
54+
end
55+
end
56+
end
57+
58+
describe '#expect_warn_deprecation' do
59+
it 'passes when there was a deprecation warning' do
60+
in_sub_process do
61+
expect_warn_deprecation(/bar/)
62+
deprecate!('bar')
63+
end
64+
end
65+
66+
pending 'fails when there were no deprecations' do
67+
in_sub_process do
68+
expect {
69+
expect_warn_deprecation(/bar/)
70+
}.to raise_error(/received: 0 times/)
71+
end
72+
end
73+
74+
# FIXME: WTH in_sub_process is needed for the new implementation, but not
75+
# for the old one?
76+
it 'fails with a MockExpectationError when there was also an ExpectationNotMetError' do
77+
in_sub_process do
78+
expect {
79+
expect_warn_deprecation(/bar/)
80+
deprecate!('bar')
81+
raise_unrelated_expectation!
82+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
83+
end
84+
end
85+
86+
it 'fails when deprecation message is different' do
87+
in_sub_process do
88+
expect {
89+
expect_warn_deprecation(/bar/)
90+
deprecate!('foo')
91+
}.to raise_error(%r{match /bar/})
92+
end
93+
end
94+
95+
it 'fails when deprecation message is different and an ExpectationNotMetError was raised' do
96+
in_sub_process do
97+
expect {
98+
expect_warn_deprecation(/bar/)
99+
deprecate!('foo')
100+
raise_unrelated_expectation!
101+
}.to raise_error(%r{match /bar/})
102+
end
103+
end
104+
end
105+
end

0 commit comments

Comments
 (0)