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

Use in built rspec warnings rather than kernel #1472

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Bug Fixes:

* When using null object doubles, prevent typos triggering dynamic matchers.
(Eric Mueller, #1455)
* Use `RSpec.warning` for an expectation warning rather than `Kernel.warn`. (Jon Rowe, #1472)

Enhancements:

Expand Down
9 changes: 4 additions & 5 deletions lib/rspec/expectations/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ module Expectations
module ExpectationHelper
def self.check_message(msg)
unless msg.nil? || msg.respond_to?(:to_str) || msg.respond_to?(:call)
::Kernel.warn [
"WARNING: ignoring the provided expectation message argument (",
msg.inspect,
") since it is not a string or a proc."
].join
RSpec.warning(
"ignoring the provided expectation message argument" \
"(#{ msg.inspect }) since it is not a string or a proc"
)
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/rspec/expectations/handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ module Expectations

expect(handle_matcher_result.error_generator.opts).to eq({ :message => "custom" })
end

it 'warns when the expectation message is not a string or proc' do
matcher = double("matcher", :matches? => true)
actual = Object.new
not_a_string_or_proc = Object.new

expect(capture_warnings {
RSpec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher, not_a_string_or_proc)
}).to contain_exactly(a_string_matching(/since it is not a string or a proc/))
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def dedent(string)
string.gsub(/^\s+\|/, '').chomp
end

def capture_warnings(&block)
warning_notifier = RSpec::Support.warning_notifier
warnings = []
RSpec::Support.warning_notifier = lambda { |warning| warnings << warning }

begin
block.call
ensure
RSpec::Support.warning_notifier = warning_notifier
end

warnings
end

# We have to use Hash#inspect in examples that have multi-entry
# hashes because the #inspect output on 1.8.7 is non-deterministic
# due to the fact that hashes are not ordered. So we can't simply
Expand Down
Loading