Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to RaiseError to ensure it passes whether or not error_highlight is active #1339

Merged
merged 1 commit into from
Jan 14, 2022
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
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 @@ -59,7 +59,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 @@ -99,7 +99,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 @@ -116,6 +116,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 @@ -144,7 +150,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