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

6

101

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.

Vaporescence answered 4/10, 2011 at 13:56 Comment(1)
Check spin.atomicobject.com/2010/12/28/rspec-backtrace-filtering There is a way to change default RSpec backtrace filteringToscanini
S
252

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

Scamander answered 4/10, 2011 at 19:51 Comment(5)
I don't get it, neither this or the accepted answer give you backtrace further then from your spec file. Or it doesn't work only for me o_OMelesa
and fwiw if you use rspec with spork and guard you can create a .rspec file and add -b to it to get the output when you run your test suiteAtlantis
This is indeed the correct answer - you don't need the full backtrace (that includes stuff like rails and rspec and other gems) almost all of the time - the only time you ever need it is to debug or understand something caused by the gem itself. So stick to the default backtrace clean patterns, and use -b in the odd case when you need it.Centurial
It didn't show stack trace info about the error itself. Rather, it showed the stack trace of the rspec gemNodular
Works for RSpec 3 as wellSelfrestraint
C
29

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.

Crossbow answered 2/11, 2011 at 20:10 Comment(4)
Yes! this is exactly what I needed. stack trace to the actual error not the spec stack.Granduncle
I believe the appropriate spelling is color. I think if you spell it colour a Monty Python joke might appearCranberry
Appropriate if you are from America. In the UK they write colour, and the rspec folks are so kind to allow both ;)Crossbow
That's thrown a spanner in the bonnet!Whirlybird
Y
6

This will also work:

# rails_helper.rb
RSpec.configure do |config|
  config.full_backtrace = true
end
Yoicks answered 5/1, 2017 at 5:56 Comment(0)
M
2

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
Masefield answered 14/5, 2014 at 15:41 Comment(1)
Doesn't seem to have that option in rspec 2.x.Waziristan
A
1

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).

Anthropopathy answered 23/9, 2014 at 4:11 Comment(0)
W
0

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 ""
Waziristan answered 16/6, 2014 at 20:28 Comment(2)
This is sometimes the best option. Just make sure you remove it once you get it working. It should wrap the place where you expect the error to be. Perhaps put the begin and rescue block as the outer statement of the controller's action method. Or use rescue_from.Anthropopathy
@bat yes this is a last resort attempt and like @bat said needs to be removed once you figured it out. Always remember to do your git diff before committing it will save your bacon.Waziristan

© 2022 - 2024 — McMap. All rights reserved.