Suppress Ruby warnings when running specs

Ruby on-RailsRubyRspecWarnings

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

The syntax for RUBYOPT is

RUBYOPT="-W0" rspec

Tested in ruby 2.1.x and 2.14.x

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

Putting Warning[:deprecated] = false after require "rails/all" in config/application.rb works very well to suppress those warnings everywhere. You can do

Warning[:deprecated] = false if Rails.env.test?

for your particular case, or better yet - put it in config/environments/test.rb, but I'm not sure how well it is going to work since I belive some stuff is loaded before that.

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

If you have this in your .rspec file, remove

--warnings

from your .rspec file in your project root.

Solution 8 - Ruby on-Rails


rspec has a tag option you can use -- I simply used /dev/null.

rspec spec --deprecation-out /dev/null

Solution 9 - Ruby on-Rails

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)

Solution 10 - Ruby on-Rails

If you're using guard for tests and Rails 6 and you get warnings such as:

  • "warning: FILE in eval may not return location in binding"
  • "warning: Capturing the given block using Proc.new is deprecated; use &block instead"
  • "warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call"

Then the only way so remove them all is to:

  1. Add $VERBOSE = nil to config/environments/test.rb
  2. Run guard with: RUBYOPT='-W0' bundle exec guard

I guess this is not good advice to remove all those warnings so later on, after a few gem updates, we should remove those lines again so that we get the right warnings on your own code usage for example.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJey BalachandranView Question on Stackoverflow
Solution 1 - Ruby on-RailsDingleView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJakob SView Answer on Stackoverflow
Solution 3 - Ruby on-RailsG. I. JoeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsScott PattenView Answer on Stackoverflow
Solution 5 - Ruby on-RailsJerry WisdomView Answer on Stackoverflow
Solution 6 - Ruby on-RailsHenrique Panham Junqueira VillView Answer on Stackoverflow
Solution 7 - Ruby on-RailsLenin Raj RajasekaranView Answer on Stackoverflow
Solution 8 - Ruby on-RailsmeditatingCybermindView Answer on Stackoverflow
Solution 9 - Ruby on-RailsRudy RigotView Answer on Stackoverflow
Solution 10 - Ruby on-RailsvvoView Answer on Stackoverflow