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

Commit

Permalink
Merge pull request #1339 from petergoldstein/feature/raise_error_fix_…
Browse files Browse the repository at this point in the history
…for_ruby_3_1

Changes to RaiseError to ensure it passes whether or not error_highlight is active
  • Loading branch information
JonRowe committed Jan 14, 2022
1 parent 6322407 commit f2d8a8a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions lib/rspec/matchers/built_in/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def matches?(given_proc, negative_expectation=false, &block)
given_proc.call
rescue Exception => @actual_error
if values_match?(@expected_error, @actual_error) ||
values_match?(@expected_error, @actual_error.message)
values_match?(@expected_error, actual_error_message)
@raised_expected_error = true
@with_expected_message = verify_message
end
Expand Down Expand Up @@ -97,7 +97,7 @@ def expects_call_stack_jump?
# @api private
# @return [String]
def failure_message
@eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
@eval_block ? actual_error_message : "expected #{expected_error}#{given_error}"
end

# @api private
Expand All @@ -114,6 +114,12 @@ def description

private

def actual_error_message
return nil unless @actual_error

@actual_error.respond_to?(:original_message) ? @actual_error.original_message : @actual_error.message
end

def expectation_matched?
error_and_message_match? && block_matches?
end
Expand Down Expand Up @@ -142,7 +148,7 @@ def eval_block

def verify_message
return true if @expected_message.nil?
values_match?(@expected_message, @actual_error.message.to_s)
values_match?(@expected_message, actual_error_message.to_s)
end

def warn_for_negative_false_positives!
Expand Down
4 changes: 2 additions & 2 deletions spec/rspec/matchers/built_in/raise_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def to_s
it "fails if any other error is raised with the wrong message" do
expect do
expect { raise NameError.new('blarg') }.to raise_error('blah')
end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg>/)
end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg/)
end

it 'includes the backtrace of any other error in the failure message' do
Expand Down Expand Up @@ -286,7 +286,7 @@ def to_s
it "fails if any other error is raised with the wrong message" do
expect do
expect { raise NameError.new('blarg') }.to raise_error.with_message('blah')
end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg>/)
end.to fail_with(/expected Exception with \"blah\", got #<NameError: blarg/)
end
end

Expand Down

0 comments on commit f2d8a8a

Please sign in to comment.