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

Commit 34b2961

Browse files
committed
Ensure backtraces start at user code, not rspec-mocks code
Addresses #1592
1 parent 134b1ac commit 34b2961

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/rspec/mocks/error_generator.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,24 @@ def differ
328328
def __raise(message, backtrace_line=nil, source_id=nil)
329329
message = opts[:message] unless opts[:message].nil?
330330
exception = RSpec::Mocks::MockExpectationError.new(message)
331+
filter_backtrace!(exception)
331332
prepend_to_backtrace(exception, backtrace_line) if backtrace_line
332333
notify exception, :source_id => source_id
333334
end
334335

336+
def filter_backtrace!(exception)
337+
backtrace = exception.backtrace || caller
338+
source = backtrace.rindex { |l| l =~ /#{File::SEPARATOR}rspec-mocks(-[^#{File::SEPARATOR}]+)?#{File::SEPARATOR}/ }
339+
340+
unless source.nil?
341+
backtrace = backtrace[(source+1)..-1]
342+
end
343+
344+
unless backtrace.empty?
345+
exception.set_backtrace(backtrace)
346+
end
347+
end
348+
335349
if RSpec::Support::Ruby.jruby?
336350
def prepend_to_backtrace(exception, line)
337351
raise exception

0 commit comments

Comments
 (0)