2011-02-03 15 views
1

私はRuby on Rails初心者や筆記試験を行っています。これらのうちのいくつかは例外を生成します。私は "レイクテスト"の出力が私に例外エラーメッセージを与えるが、全体のバックトレースは与えないことを望む。 (私は、その後に記入します未実装の機能を実行するテストを書きたいと思います。)Railsがエラーをテストしたときのスタックトレースを抑制する

を例えば、実際の出力:

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 

所望の出力:

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反対方向には情報(e.g.)が見つかりましたが、スタックトレースは抑制されませんでした。

編集:

オンとオフを簡単にそれらを有効にするとよいでしょう。下記のように、バグを追跡するのに便利なことがあります。 - あなたは「バックトレースサイレンサー」を見て取ることができる

+0

OK、それはスタックトレースは、テキストフィクスチャの不良データから来ていることがわかります。修復されて、出力は私が期待している通りです。間違った前提に基づいた質問に対処するためのエチケットは何ですか? – Reid

答えて

1

私にとっては(Railsの2.3.8)、これはファイルの設定/初期化子/ 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/} 

あなたができるはずであるように見えますあなたのconfig /環境/ test.rbファイルで

Rails.backtrace_cleaner.add_silencer {|line| true} 

ような行を置くために、それはあなたのバックトレースが離れてきれいに拭くでしょう(それはちょうどロガーに適用するかもしれませんが - 私はこの方法に精通していませんよ)。

しかし、自分自身に質問してください。本当にバックトレースを完全に取り除きたいのですか?彼らはかなりバグを追跡するのに便利なことができます...

+0

ありがとう@ xavier。これらは便利ですが、エラーが発生しているいくつかのテストがある場合、出力量は圧倒的です。 – Reid

関連する問題