Display full backtraces in rspec

less than 1 minute read

I use rspec a lot. I thinks it’s a great testing framework.

In order to get shorter error output, rspec cleans backtraces from outside code. Here are all the patterns that are removed from the full backtrace :

DEFAULT_BACKTRACE_PATTERNS = [
  /\/lib\d*\/ruby\//,
  /org\/jruby\//,
  /bin\//,
  %r|/gems/|,
  /spec\/spec_helper\.rb/,
  /lib\/rspec\/(core|expectations|matchers|mocks)/
]

Most of the time, that’s great. Sometimes though, we get messages that seem completly unrelated to the code. Even worse, we might get misleading messages, such as “method called with unexpected number of arguments” that does not refer to the code in the backtrace, but to some other library code …

I thought it would be nice if we could switch this cleaning off sometimes. Here is how I did this :

RSpec.configure do |config|

  # RSpec automatically cleans stuff out of backtraces;
  # sometimes this is annoying when trying to debug something e.g. a gem
  if ENV['FULLBACKTRACES'] == 'true'
    config.backtrace_clean_patterns = []
  end

  # some other configuration here

end

This way, it is still possible to call rspec as before to get the standard behaviour, but it is now possible to specify the FULLBACKTRACES variable to get full backtraces.

FULLBACKTRACES=true bundle exec rspec spec

I usually write about 15 minutes worth of reading per month. I won't transfer your email. No Spam, unsubscribe whenever you want.

As a gift for subscribing, you'll receive an illustrated mini-ebook "How to start a team coding dojo"!

Leave a comment