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

Commit b3a075a

Browse files
authored
Merge pull request #452 from rspec/fix-deprecation-helpers
Fix expect_warn_deprecation matching any message
2 parents 7774664 + 7ae427d commit b3a075a

File tree

2 files changed

+114
-29
lines changed

2 files changed

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

0 commit comments

Comments
 (0)