From 25e8c6b9c95188ab17601cd1caea50fad1001e45 Mon Sep 17 00:00:00 2001 From: Steven Willis Date: Tue, 17 Sep 2024 12:19:33 -0400 Subject: [PATCH] Ensure backtraces start at user code, not rspec-mocks code Addresses #1592 --- lib/rspec/mocks/error_generator.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/rspec/mocks/error_generator.rb b/lib/rspec/mocks/error_generator.rb index 8e7e2c1fc..5191ee9a6 100644 --- a/lib/rspec/mocks/error_generator.rb +++ b/lib/rspec/mocks/error_generator.rb @@ -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