Suppress Ruby warnings when running specs

前端 未结 10 2259
野趣味
野趣味 2020-11-27 14:46

I\'m looking for a way to suppress Ruby warnings when I run my specs.

spec spec/models/account_spec.rb

I receive warnings such as:

10条回答
  •  佛祖请我去吃肉
    2020-11-27 15:26

    If you run your specs directly with the ruby command instead of the spec wrapper, you can use the -W command line option to silence warnings:

    $ ruby --help
    [...]
      -W[level]       set warning level; 0=silence, 1=medium, 2=verbose (default)
    

    So in your case:

    $ ruby -W0 -Ispec spec/models/event_spec.rb
    

    should not show you any warnings.

    Alternatively, you could set $VERBOSE=nil before your gems are loaded, ie at the top of your environment.rb (or application.rb if you're on Rails 3). Note that this disables all warnings all the time.

    Or, since you are using Rails, you should be able to use Kernel.silence_warnings around the Bundler.require block if you're using Bundler:

    Kernel.silence_warnings do
      Bundler.require(:default, Rails.env) if defined?(Bundler)
    end
    

    More selectively, set $VERBOSE only for loading specific gems:

    config.gem 'wellbehaving_gem'
    original_verbosity = $VERBOSE
    $VERBOSE = nil
    config.gem 'noisy_gem_a'
    $VERBOSE = original_verbosity
    

提交回复
热议问题