Suppress Ruby warnings when running specs

2020-01-25 05:30发布

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:

DEPRECATION WARNING: ActiveSupport::Dependencies.load_paths is deprecated, ...
warning: already initialized constant SOME_CONSTANT_NAME

Removing the ActiveSupport warning is quite easy with ActiveSupport::Deprecation.silenced = true.

How do I prevent the already initialized constant warnings as part of my spec command? Or through creating another spec file that can suppress such warnings. Keep in mind that these warnings are from gem files, therefore I cannot go into those files and surround them with Kernel.silence_warnings.

Note: I understand that suppressing warnings are bad. However, when I run a single spec from within vim it would be nice if the warnings didn't clutter my screen.

10条回答
Rolldiameter
2楼-- · 2020-01-25 06:04

Related with this post, you can manage deprecation warnings according to th environment in which you are working, as said in rails guides:

active_support.deprecation_behavior Sets up deprecation reporting for environments, defaulting to :log for development, :notify for production and :stderr for test. If a value isn't set for config.active_support.deprecation then this initializer will prompt the user to configure this line in the current environment's config/environments file. Can be set to an array of values.

So just change in config/environments/test.rb the value :stderr for :log

Rails.application.configure do
   ...
   # Print deprecation notices to the log file instead of console.
   config.active_support.deprecation = :log
   ...
end

And with this change, the deprecation warnings will now be printed to the log/test.log instead of the console output.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-01-25 06:07

Actually, perhaps you shouldn't ignore your warnings, but test them, to make sure they are fired where they're supposed to be.

It's not the easiest to use, but it looks like this:

obj.should_receive(:warn).with("Some Message")

I found it here, and tested it for my use case, and it works (and the warnings disappear from the console, of course)

查看更多
成全新的幸福
4楼-- · 2020-01-25 06:10

The only solution that worked for me is to add $VERBOSE = nil on top of my config/environments/test.rb file

  Rails.application.configure do
   $VERBOSE = nil

I'm with faker warning problems faker-1.9.6/lib/faker/default/number.rb:34. Use it local because it hides all other warnings.

查看更多
萌系小妹纸
5楼-- · 2020-01-25 06:18

If you have this in your .rspec file, remove

--warnings

from your .rspec file in your project root.

查看更多
萌系小妹纸
6楼-- · 2020-01-25 06:20

You can also use the "RUBYOPT" environment variable to pass -W0 to rspec:

RUBYOPT=W0 rspec spec/models/event_spec.rb

This allows you to run multiple specs by passing in a directory

RUBYOPT=W0 rspec spec/models
查看更多
\"骚年 ilove
7楼-- · 2020-01-25 06:21

The syntax for RUBYOPT is

RUBYOPT="-W0" rspec

Tested in ruby 2.1.x and 2.14.x

查看更多
登录 后发表回答