How to Show Longer Traces in Rails Testcases

How to show longer traces in Rails TestCases

See whether you haven't a file config/initializers/backtrace_silencers.rb

It says:

# You can also remove all the silencers if you're trying to debug
# a problem that might stem from framework code.
Rails.backtrace_cleaner.remove_silencers!

You can also call remove_filters! if the first was not enough, but filters only make the paths shorter, while silencers do remove some lines from the backtrace.

You may find the source code in railties/lib/rails/backtrace_cleaner.rb and activesupport/lib/active_support/backtrace_cleaner.rb useful.

Turn on full backtrace in Ruby on Rails TestCase

BACKTRACE=blegga rake test

BACKTRACE=blegga rails test # rails 5+

Append --trace if you need rake related log.

How do I get ruby to print a full backtrace instead of a truncated one?

Exception#backtrace has the entire stack in it:

def do_division_by_zero; 5 / 0; end
begin
do_division_by_zero
rescue => exception
puts exception.backtrace
raise # always reraise
end

(Inspired by Peter Cooper's Ruby Inside blog)

Trace source of deprecation warnings in rails tests

To solve this you could enable the full debugging information. (see the help)

ActiveSupport::Deprecation.debug = true

As @Eric Anderson says it should be placed after Rails loads (i.e. after require 'rails/all' in application.rb) but before bundler runs to catch deprecation warning in gems (i.e. before Bundler.require(:default, Rails.env) if defined?(Bundler) in application.rb).

You can add a condition, like if ENV["DEBUG"] or if environment == :test to leave this in your config.

How do I log every method that's called in a Ruby program?

This is definitely possible -- in fact, there's even a method for it! Just add this somewhere in your code before the point that you want to start logging things:

set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}

The secret sauce you want comes from Kernel#set_trace_func, as noted above:

  • set_trace_func(proc) => proc
  • set_trace_func(nil) => nil

Establishes proc as the handler for tracing, or disables tracing if the parameter is nil. proc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a class. proc is invoked whenever an event occurs. Events are: c-call (call a C-language routine), c-return (return from a C-language routine), call (call a Ruby method), class (start a class or module definition), end (finish a class or module definition), line (execute code on a new line), raise (raise an exception), and return (return from a Ruby method). Tracing is disabled within the context of proc.

Here's a handy example:

class Test
def test
a = 1
b = 2
end
end

set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}

t = Test.new
t.test

(Note: don't try this in irb unless you want a huge scrolling screen of text.) The resulting output is:

    line test.rb:11               false
c-call test.rb:11 new Class
c-call test.rb:11 initialize Object
c-return test.rb:11 initialize Object
c-return test.rb:11 new Class
line test.rb:12 false
call test.rb:2 test Test
line test.rb:3 test Test
line test.rb:4 test Test
return test.rb:4 test Test

You can play around with the formatting string above to get just the results you want to log (for example, it sounds like you're only interested in call events).

Rails Testing Error - NameError: TestCase::SUPPORTS_INFO_SIGNAL

I just came across a bug that is similar or identical to this one and was able to resolve it locally. For me, I had a trace that begins with:

/home/youruser/.rvm/gems/ruby-2.1.0/gems/mocha-0.10.5/lib/mocha/integration/mini_test/version_230_to_262.rb:19:in `run': uninitialized constant MiniTest::Unit::TestCase::SUPPORTS_INFO_SIGNAL (NameError)

This shows that the error is actually occurring in mocha when it tries to reference a constant that has been removed from the version of minitest that likely came along with your Ruby upgrade.

For me, doing a bundle update mocha fixed the problem.

Doing a bit of code spelunking to figure out specifically where the problem was fixed, I used the "git pickaxe" (git log -SSUPPORTS_INFO_SIGNAL) on a cloned copy of the mocha repository, and it seems like this commit removed the reference to SUPPORTS_INFO_SIGNAL, so using anything after that commit in mocha should be ok.

If upgrading your version of mocha doesn't solve the problem, take a close look at the stack trace and you should see which library that depends minitest having SUPPORTS_INFO_SIGNAL defined is causing the error. Then look upstream in that dependency to see if this problem was fixed. If not, fork the dependency you're using that exhibits this behavior and push your own patch upstream.



Related Topics



Leave a reply



Submit