5

当出现错误时,有什么方法可以减少 Ruby 提供的输出量?

例如:

rspec bowling_spec.rb 
/Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bowling (LoadError)
  from /Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
  from /Users/snowcrash/Developer/Code/Ruby/RSpec/bowling_spec.rb:2:in `<top (required)>'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `each'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load_spec_files'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/command_line.rb:22:in `run'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:80:in `run'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:17:in `block in autorun'

我感兴趣的是第一行,cannot load such file -- bowling (LoadError). 理想情况下,我希望 Ruby 不要吐出其余的from行。

这可能吗?

4

2 回答 2

2

做这样的事情:

module Kernel
  at_exit do
    case $!
    when nil, SystemExit, Interrupt
    else puts $!.message, $@.first
    end
    $stderr.reopen(IO::NULL)
    $stdout.reopen(IO::NULL)
  end
end
于 2013-07-10T19:16:51.193 回答
0

如果您在 Unix/Linux 操作系统上,并且您正在从命令行运行测试,您可以这样做

rspec bowling_spec.rb | head -n 20

以确保您不必向上滚动即可查看错误。

于 2013-07-26T00:49:45.390 回答