Rails 测试中弃用警告的追踪来源
运行功能测试时,我在其中一个测试用例中收到以下警告,但我无法确定它的来源:
gems/actionpack-2.3.8/lib/action_controller/record_identifier.rb:76 :警告:Object#id 将被弃用; 不幸的是,这是显示的唯一一行回溯,即使我使用
rake test --trace 运行它,并且 log/ 中没有更多信息测试.log。
如何获得此警告的完整回溯或以其他方式找出代码中的哪一行导致了该警告?
When running my functional tests, I'm getting the following warning in one of the test cases but I can't pinpoint where it's coming from:
gems/actionpack-2.3.8/lib/action_controller/record_identifier.rb:76: warning: Object#id will be deprecated; use Object#object_id
Unfortunately that's the only line of the backtrace that's shown, even if I run it with rake test --trace
, and there is no more information in log/test.log
.
How can I get the full backtrace for this warning or otherwise figure out which line in my code is causing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此操作的新方法是:
The new way of doing this is:
当我在测试中收到此类警告时,通常是因为我正在使用模拟模型对象,并且没有提供活动记录真正提供的所有方法。
一个好的起点是 Rails 代码本身。查看引用的
action_pack
gem 的源代码,导致错误的方法是dom_id
。该方法为页面上使用的对象生成一个 id。它似乎在内部的几个地方被调用(当然,除非您直接调用它!),但最可能的原因似乎是在对象上调用form_for
。When I get this kind of warning in my tests it is usually because I am using mocking model objects and am not providing all the methods that active record provides for real.
A good starting point would be the rails code itself. Looking at the source code for the
action_pack
gem which is referenced, the method that is causing the error isdom_id
. That method generates an id for an object for use on a page. It seems to be called in a couple of places internally (unless you are calling it directly of course!) but the most likely cause appears to be callingform_for
on an object.有同样的问题。一个 gem 导致了弃用警告,但我不知道是哪个 gem,因为 Rail 的消息仅显示我的代码中调用堆栈的最后一位。添加以下内容:
将其放置在 Rails 加载之后(即在 application.rb 中的
require 'rails/all'
之后),但在 bunder 运行以捕获 gems 中的弃用警告之前(即在Bundler.require( :default, Rails.env) 如果已定义?(Bundler)
in application.rb)。现在,当遇到弃用警告时,您将被放入调试器中。您可以将其保留(并用
if Rails.env.test?
包围),也可以在发现问题后将其删除。Had the same issue. A gem was causing a deprecation warning but I had no idea which gem since Rail's message only shows the last bit of the callstack in my code. Add the following:
Placed this after Rails loads (i.e. after
require 'rails/all'
in application.rb) but before bunder runs to catch deprecation warning in gems (i.e. beforeBundler.require(:default, Rails.env) if defined?(Bundler)
in application.rb).Now when a deprecation warning is encountered you are dropped in the debugger. You can either leave this in (and surround with a
if Rails.env.test?
) or remove it when you have found your issues.更新:如 @Dorian 提到该方法在最新的 Rails 版本中已被弃用,因此您应该使用:
要解决此问题,您可以启用完整的调试信息。 (请参阅帮助)
正如 @Eric Anderson 所说,它应该放在 Rails 之后加载(即在 application.rb 中的
require 'rails/all'
之后)但在捆绑器运行以捕获 gems 中的弃用警告之前(即在Bundler.require(:default, Rails.env 之前)如果已定义?(Bundler)
in application.rb)。您可以添加一个条件,例如
if ENV["DEBUG"]
或ifenvironment == :test
将其保留在您的配置中。Update: as @Dorian mentions the method is deprecated on recent Rails versions, so you should use:
To solve this you could enable the full debugging information. (see the help)
As @Eric Anderson says it should be placed after Rails loads (i.e. after
require 'rails/all'
in application.rb) but before bundler runs to catch deprecation warning in gems (i.e. beforeBundler.require(:default, Rails.env) if defined?(Bundler)
in application.rb).You can add a condition, like
if ENV["DEBUG"]
orif environment == :test
to leave this in your config.