Skip to content

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 25e8c6b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/rspec/mocks/error_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,20 @@ def differ
def __raise(message, backtrace_line=nil, source_id=nil)
message = opts[:message] unless opts[:message].nil?
exception = RSpec::Mocks::MockExpectationError.new(message)
set_filtered_backtrace(exception)
prepend_to_backtrace(exception, backtrace_line) if backtrace_line
notify exception, :source_id => source_id
end

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

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

0 comments on commit 25e8c6b

Please sign in to comment.