如何在 RSpec while 运行 Ruby 2.7.0 中抑制警告

How to Suppress Warnings in RSpec while running Ruby 2.7.0

安装 Ruby 2.7.0 后 运行 specs 变成了警告的噩梦,例如这个:

/Users/user/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/capybara-3.30.0/lib/capybara/node/matchers.rb:835: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/user/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/capybara-3.30.0/lib/capybara/node/matchers.rb:861: warning: The called method `_set_query_session_options' is defined here

由于这些警告是从 gems 中弹出的,它们确实没有帮助并使 RSpec 输出一团糟。

我尝试将此行添加到 spec_helper.rb

config.warnings = false

而这一行到config/environments/test.rb

config.active_support.deprecation = :log

但是,警告仍然成百上千地弹出。我能做些什么来摆脱它们吗?

运行 Ruby Rails 6.0.2.1 和 Ruby 2.7.0

注意 已经提示这个问题的答案已经存在于Suppress Ruby warnings when running specs 这看起来是一个类似的问题,但是提供的解决方案没有效果。我看到了所有警告。

这些 warnings/deprecations 直接来自 ruby 2.7,您不能通过 rails 或 rspec 配置将它们静音。

Ruby 2.7 正试图警告您 ruby 3.0 中将出现的向后不兼容问题。参见release notes。弃用的主要来源是 位置参数和关键字参数的分离 部分。

Rails 和其他库和 gem 尚未准备好应对此更改,因此 ruby 向您显示大量警告。

我会等待升级,直到 gems 将来解决这些警告,但您也可以根据以下文章抑制这些警告 https://prathamesh.tech/2019/12/26/managing-warnings-emitted-by-ruby-2-7/

RUBYOPT='-W:no-deprecated -W:no-experimental' rails c