运行规范时禁止 Ruby 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5591509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suppress Ruby warnings when running specs
提问by Jey Balachandran
I'm looking for a way to suppress Ruby warnings when I run my specs.
我正在寻找一种在运行规范时抑制 Ruby 警告的方法。
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 ActiveSupportwarning is quite easy with ActiveSupport::Deprecation.silenced = true.
拆卸ActiveSupport警告是很容易的ActiveSupport::Deprecation.silenced = true。
How do I prevent the already initialized constant warnings as part of my speccommand? Or through creating another specfile 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.
作为spec命令的一部分,如何防止已经初始化的常量警告?或者通过创建另一个spec可以抑制此类警告的文件。请记住,这些警告来自 gem 文件,因此我无法进入这些文件并用Kernel.silence_warnings.
Note:I understand that suppressing warnings are bad. However, when I run a single specfrom within vimit would be nice if the warnings didn't clutter my screen.
注意:我知道抑制警告是不好的。但是,当我spec从内部运行单曲时,vim如果警告不会使我的屏幕混乱,那就太好了。
回答by Jakob S
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 命令而不是规范包装器运行规范,则可以使用 -W 命令行选项来消除警告:
$ 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.
或者,您可以在加载 gem 之前设置 $VERBOSE=nil,即在您的 environment.rb(或 application.rb,如果您使用 Rails 3)的顶部。请注意,这会始终禁用所有警告。
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:
或者,由于您使用的是 Rails,如果您使用 Bundler,您应该能够在 Bundler.require 块周围使用 Kernel.silence_warnings:
Kernel.silence_warnings do
Bundler.require(:default, Rails.env) if defined?(Bundler)
end
More selectively, set $VERBOSE only for loading specific gems:
更有选择性地,设置 $VERBOSE 仅用于加载特定的 gem:
config.gem 'wellbehaving_gem'
original_verbosity = $VERBOSE
$VERBOSE = nil
config.gem 'noisy_gem_a'
$VERBOSE = original_verbosity
回答by Dingle
The syntax for RUBYOPTis
的语法RUBYOPT是
RUBYOPT="-W0" rspec
Tested in ruby 2.1.x and 2.14.x
在 ruby 2.1.x 和 2.14.x 中测试
回答by G. I. Joe
Related with this post, you can manage deprecation warnings according to th environment in which you are working, as said in rails guides:
与这篇文章相关,您可以根据您工作的环境管理弃用警告,如rails 指南中所述:
active_support.deprecation_behaviorSets up deprecation reporting for environments, defaulting to :logfor development, :notifyfor productionand :stderrfor 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/environmentsfile. Can be set to an array of values.
active_support.deprecation_behavior设置折旧报告的环境中,默认为:日志的发展,:通知对 生产和:标准错误的测试。如果未为 config.active_support.deprecation 设置值,则此初始化程序将提示用户在当前环境的config/environments文件中配置此行 。可以设置为一组值。
So just change in config/environments/test.rbthe value :stderrfor :log
因此,只要在改变config/environments/test.rb值:标准错误的日志:
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.loginstead of the console output.
通过此更改,弃用警告现在将打印到log/test.log控制台输出而不是控制台输出。
回答by Scott Patten
You can also use the "RUBYOPT" environment variable to pass -W0 to rspec:
您还可以使用“RUBYOPT”环境变量将 -W0 传递给 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
回答by Lenin Raj Rajasekaran
If you have this in your .rspecfile, remove
如果您的.rspec文件中有这个,请删除
--warnings
from your .rspecfile in your project root.
从您.rspec的项目根目录中的文件。
回答by meditatingCybermind
rspec has a tag option you can use -- I simply used /dev/null.
rspec 有一个你可以使用的标签选项——我只是使用了/dev/null。
rspec spec --deprecation-out /dev/null
回答by Henrique Panham Junqueira Vill
The only solution that worked for me is to add $VERBOSE = nilon top of my config/environments/test.rb file
对我$VERBOSE = nil有用的唯一解决方案是在我的 config/environments/test.rb 文件之上添加
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.
我遇到了更假的警告问题faker-1.9.6/lib/faker/default/number.rb:34。在本地使用它,因为它隐藏了所有其他警告。
回答by Jerry Wisdom
Putting Warning[:deprecated] = falseafter require "rails/all"in config/application.rbworks very well to suppress those warnings everywhere. You can do
把Warning[:deprecated] = false以后require "rails/all"的config/application.rb作品很好地到处抑制这些警告。你可以做
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.
对于您的特定情况,或者更好 - 将其config/environments/test.rb放入 .
回答by Rudy Rigot
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)
我在这里找到了它,并在我的用例中对其进行了测试,它可以工作(当然,警告从控制台中消失了)
回答by vvo
If you're using guard for tests and Rails 6 and you get warnings such as:
- "warning: FILEin eval may not return location in binding"
- "warning: Capturing the given block using Proc.new is deprecated; use &blockinstead"
- "warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call"
如果您在测试和 Rails 6 中使用守卫,并且您收到以下警告:-“警告:eval 中的FILE可能不会返回绑定中的位置”-“警告:不推荐使用 Proc.new 捕获给定块;&block改为使用” - “警告:不推荐使用最后一个参数作为关键字参数;也许 ** 应该添加到调用中”
Then the only way so remove them all is to:
那么将它们全部删除的唯一方法是:
- Add
$VERBOSE = niltoconfig/environments/test.rb - Run guard with:
RUBYOPT='-W0' bundle exec guard
- 添加
$VERBOSE = nil到config/environments/test.rb - 运行警卫:
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.
我想这不是删除所有这些警告的好建议,所以稍后,在几次 gem 更新之后,我们应该再次删除这些行,以便我们获得有关您自己的代码使用的正确警告。

