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

帅比萌擦擦* 提交于 2019-11-28 17:20:57
solnic

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

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.

This will also work:

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

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

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 ""

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!