diff --git a/lib/rspec/mocks/error_generator.rb b/lib/rspec/mocks/error_generator.rb index 8e7e2c1fc..7fbe92b86 100644 --- a/lib/rspec/mocks/error_generator.rb +++ b/lib/rspec/mocks/error_generator.rb @@ -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