How do I shorten the backtrace for a test failure in RSpec 2?
Asked Answered
D

2

18

When my specs hit an error, I get a message like this:

Vendor should reject duplicate names
     Failure/Error: user_with_duplicate_email.should_not be_valid
     expected valid? to return false, got true
     # /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/gems/rspec-expectations-2.3.0/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
     # /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/gems/rspec-expectations-2.3.0/lib/rspec/expectations/handler.rb:44:in `handle_matcher'
     # /home/kevin/.rvm/gems/ruby-1.9.2-p136@rails3tutorial/gems/rspec-expectations-2.3.0/lib/rspec/expectations/extensions/kernel.rb:50:in `should_not'
.
.
about 15 more lines
.
.
     # /home/kevin/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop'
     # /home/kevin/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop'
     # /home/kevin/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/drb/drb.rb:1588:in `block in main_loop'

I'm running Ruby 1.0.2, rails (3.0.3), and rspec (2.3.0). M .rspec configuration file has only two options specified:

--drb --colour

How do I turn off the extended trace?

Dollie answered 19/2, 2011 at 4:0 Comment(0)
A
13

In spec_helper.rb you can filter backtrace using the following code snippet:

RSpec.configure do |config|
  # RSpec automatically cleans stuff out of backtraces;
  # sometimes this is annoying when trying to debug something e.g. a gem

  # RSpec 3:
  # config.backtrace_exclusion_patterns = [
  # RSpec 2:
  config.backtrace_clean_patterns = [
    /\/lib\d*\/ruby\//,
    /bin\//,
    /gems/,
    /spec\/spec_helper\.rb/,
    /lib\/rspec\/(core|expectations|matchers|mocks)/
  ]
end
Aubyn answered 2/4, 2012 at 22:4 Comment(0)
T
3

I updated it to work with with Rspec 3.2.3. In spec_helper.rb put:

RSpec.configure do |config|
  config.backtrace_exclusion_patterns = [
    /\/lib\d*\/ruby\//,
    /bin\//,
    /gems/,
    /spec\/spec_helper\.rb/,
    /lib\/rspec\/(core|expectations|matchers|mocks)/
  ]
end

This is pure gold. Thank you luacassus!

Tandem answered 23/12, 2015 at 19:21 Comment(1)
Why is this not working for me? I know the code loads because I've used puts 'HERE' above this config and I see it, but I still get super long backtrace.Counteract

© 2022 - 2024 — McMap. All rights reserved.