Search code examples
rspecrspec-rails

rspec not providing any backtrace


I'm trying to debug something with rspec, but the stack traces just aren't helpful: they don't go deep enough.

NoMethodError:
   undefined method `after_create=' for #<Spree::Calculator::FlatRate:0x007ffc988d1868>
 # ./spec/models/stripe_event_processor_spec.rb:63:in `block (2 levels) in <top (required)>'
 # ./spec/models/stripe_event_processor_spec.rb:69:in `block (2 levels) in <top (required)>'
 # ./spec/models/stripe_event_processor_spec.rb:81:in `block (2 levels) in <top (required)>'

Here, for instance, there's a problem within a FactoryGirl method in one of my gems, but all that rspec is giving me is the line in my own test which is triggering it. Not super helpful.

I've tried adding --backtrace to my rspec command, but it doesn't make the trace any longer!

I suspect, looking at this article, that this may be because the default rspec config filters the stack trace when it gets into the gems/ folder.

The article suggests just overriding the rspec config manually, which is a bit hacky, but may be the best solution. If that's true: then where does said rspec config reside on my computer? It's not in my bundler directory with all of my other gems.

Any ideas? How can I get rspec to give me a real backtrace, gems and all?


Solution

  • Turns out that I don't need to find out where on my computer the config.backtrace_clean_patterns is being defined, as I can override it in my spec_helper.rb as follows:

    RSpec.configure do |config|
      # RSpec automatically cleans stuff out of backtraces;
      # sometimes this is annoying when trying to debug something e.g. a gem
      config.backtrace_clean_patterns = [
        /\/lib\d*\/ruby\//,
        /bin\//,
        /gems/,
        /spec\/spec_helper\.rb/,
        /lib\/rspec\/(core|expectations|matchers|mocks)/
      ]
    end
    

    is the default, and can be overridden as simply as putting into the spec_helper.rb file the following:

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

    Now I can see the full traces!