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

Commit

Permalink
Ensure backtraces start at user code, not rspec-mocks code
Browse files Browse the repository at this point in the history
Addresses #1592
  • Loading branch information
onlynone committed Sep 17, 2024
1 parent 134b1ac commit 34b2961
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rspec/mocks/error_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,24 @@ def differ
def __raise(message, backtrace_line=nil, source_id=nil)
message = opts[:message] unless opts[:message].nil?
exception = RSpec::Mocks::MockExpectationError.new(message)
filter_backtrace!(exception)
prepend_to_backtrace(exception, backtrace_line) if backtrace_line
notify exception, :source_id => source_id
end

def filter_backtrace!(exception)
backtrace = exception.backtrace || caller
source = backtrace.rindex { |l| l =~ /#{File::SEPARATOR}rspec-mocks(-[^#{File::SEPARATOR}]+)?#{File::SEPARATOR}/ }

unless source.nil?
backtrace = backtrace[(source+1)..-1]
end

unless backtrace.empty?
exception.set_backtrace(backtrace)
end
end

if RSpec::Support::Ruby.jruby?
def prepend_to_backtrace(exception, line)
raise exception
Expand Down

0 comments on commit 34b2961

Please sign in to comment.