Rails 测试错误时抑制堆栈跟踪

发布于 2024-10-15 22:49:07 字数 1487 浏览 1 评论 0原文

我是 Ruby on Rails 新手,正在编写测试。其中一些会产生异常;我希望“rake test”输出给我异常错误消息,但不是整个回溯。 (我想编写测试来执行未实现的功能,然后我将填写这些测试。)

例如,实际输出:

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

我找到了信息(eg)在相反的方向上,但不抑制堆栈跟踪。

编辑:

如果能够轻松地打开和关闭它们就好了;如下所述,有时它们对于追踪错误很有用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-10-22 22:49:07

你可以看看“backtrace消音器” - 对我来说(Rails 2.3.8),这是文件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/}

看起来你应该能够

Rails.backtrace_cleaner.add_silencer {|line| true}

在你的config/environments/中 添加一行test.rb 文件,这会清除你的回溯(尽管它可能只适用于记录器 - 我不太熟悉该方法)。

但问问你自己 - 你真的想完全消除回溯吗?它们对于追踪错误非常有用......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文