2

I'm a Ruby on Rails newbie and writing tests. Some of these generate exceptions; I would like the "rake test" output to give me the exception error message but not the whole backtrace. (I'd like to write tests which exercise unimplemented functionality, which I'll then fill in.)

For example, actual output:

Started
E
Finished in 0.081054 seconds.

  1) Error:
test_should_fail(VersioningTest):
ActiveRecord::StatementInvalid: PGError: ERROR:  null value in column "client_ip" violates not-null constraint
: INSERT INTO "revisions" ("created_at", "id") VALUES ('2011-02-03 20:14:17', 980190962)
    /Users/rpriedhorsky/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log'
    /Users/rpriedhorsky/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:194:in `log'
    /Users/rpriedhorsky/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:496:in `execute'
    [... etc. etc. etc. ...]

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

Desired output:

Started
E
Finished in 0.081054 seconds.

  1) Error:
test_should_fail(VersioningTest):
ActiveRecord::StatementInvalid: PGError: ERROR:  null value in column "client_ip" violates not-null constraint

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

I found info (e.g.) on the opposite direction, but not on suppressing stack traces.

Edit:

It would be nice to turn them on and off easily; as pointed out below, sometimes they are useful for tracking down bugs.

Community
  • 1
  • 1
Reid
  • 1,999
  • 3
  • 17
  • 25
  • OK, so it turns out the stack traces are coming from bad data in the text fixtures; with that repaired, the output is as I expect. What's the etiquette for dealing with questions that are based on erroneous assumptions? – Reid Feb 04 '11 at 15:30

1 Answers1

2

You could take a look at "backtrace silencers" - for me (Rails 2.3.8), this is the file config/initializers/backtrace_silencers.rb:

# Be sure to restart your server when you modify this file.

# You can add backtrace silencers for libraries that you're using but
# don't wish to see in your backtraces.
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }

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

Rails.backtrace_cleaner.add_silencer {|line| line =~ /gems/}
Rails.backtrace_cleaner.add_silencer {|line| line =~ /passenger/}

It looks like you should be able to put a line like

Rails.backtrace_cleaner.add_silencer {|line| true}

In your config/environments/test.rb file, and that would wipe your backtraces clean away (though it might just apply to the logger - I'm not very familiar with the method).

But ask yourself - do you really want to do away with backtraces entirely? They can be pretty useful for tracking down bugs...

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
  • Thanks @xavier. They are useful - but if I have several tests that are erroring, the volume of output is overwhelming. – Reid Feb 04 '11 at 15:04