How to get rspec-2 to give the full trace associated with a test failure?

RubyRuby on-Rails-3TestingRspecRspec2

Ruby Problem Overview


Right now if I run my test suite using rake spec I get an error:

  1. SegmentsController GET 'index' should work Failure/Error: get 'index' undefined method `locale' for #

    ./spec/controllers/segments_controller_spec.rb:14:

    in `block (3 levels) in '

This is normal as I do have an error :)

The problem is that the trace isn't very helpful. I know it broke in segments_controller_spec.rb, line 14, but this is just where I call the test:

### segments_controller_spec.rb:14
get 'index'

I would prefer to have the actual line breaking and the complete trace, not the part in the spec folder.

Running with --trace doesn't help.

Ruby Solutions


Solution 1 - Ruby

You must run rspec with -b option to see full backtraces

Solution 2 - Ruby

Another (easier) alternative is to edit the .rspec file, and add the backtrace option. It should look somewhat like this:

--colour
--backtrace

That will give you the full backtrace. Hope this helps.

Solution 3 - Ruby

This will also work:

# rails_helper.rb
RSpec.configure do |config|
  config.full_backtrace = true
end

Solution 4 - Ruby

Another approach is to clear all backtrace exclusion patterns in spec_helper.rb. I like this solution most as I'm able to keep all RSpec settings in one place and get rid of .rspec file or explicit --backtrace in .travis.yml.

# spec_helper.rb
RSpec.configure do |config|
  config.backtrace_exclusion_patterns = []
end

Solution 5 - Ruby

I don't know how to get the controller error to show up in rspec. Sometimes it shows up but I don't know what conditions cause it to show up. Here is a way to see the error fairly quickly though:

Open another terminal session and run:

tail -f log/test.log

Then go back to the terminal session and run just the spec that had the error:

bin/rspec -b spec/requests/posts/index_spec.rb

Go back to the tail of the log and you should see the error, hopefully without too much other stuff surrounding it (because you ran the failing test by itself).

Solution 6 - Ruby

One more option when all else fails is to just add a rescue block and print out the stack try or add a binding pry statement there and use show-stack.

rescue Exception => e
  puts ""
  puts e.backtrace
  puts ""

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarcggView Question on Stackoverflow
Solution 1 - RubysolnicView Answer on Stackoverflow
Solution 2 - RubynathanvdaView Answer on Stackoverflow
Solution 3 - RubyMaria VView Answer on Stackoverflow
Solution 4 - RubyNowakerView Answer on Stackoverflow
Solution 5 - RubyBenjamin AtkinView Answer on Stackoverflow
Solution 6 - RubyMichael YagudaevView Answer on Stackoverflow