Rails 测试中弃用警告的追踪来源

发布于 2024-09-18 20:37:56 字数 260 浏览 6 评论 0原文

运行功能测试时,我在其中一个测试用例中收到以下警告,但我无法确定它的来源:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨笨の傻瓜 2024-09-25 20:38:26

执行此操作的新方法是:

Rails.application.deprecators.debug = true

The new way of doing this is:

Rails.application.deprecators.debug = true
肩上的翅膀 2024-09-25 20:38:22

当我在测试中收到此类警告时,通常是因为我正在使用模拟模型对象,并且没有提供活动记录真正提供的所有方法。

一个好的起点是 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 is dom_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 calling form_for on an object.

霊感 2024-09-25 20:38:17

有同样的问题。一个 gem 导致了弃用警告,但我不知道是哪个 gem,因为 Rail 的消息仅显示我的代码中调用堆栈的最后一位。添加以下内容:

module ActiveSupport::Deprecation
  class << self
    def deprecation_message_with_debugger(callstack, message = nil)
      debugger
      deprecation_message_without_debugger callstack, message
    end
    alias_method_chain :deprecation_message, :debugger
  end
end

将其放置在 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:

module ActiveSupport::Deprecation
  class << self
    def deprecation_message_with_debugger(callstack, message = nil)
      debugger
      deprecation_message_without_debugger callstack, message
    end
    alias_method_chain :deprecation_message, :debugger
  end
end

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. before Bundler.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.

抹茶夏天i‖ 2024-09-25 20:38:13

更新:如 @Dorian 提到该方法在最新的 Rails 版本中已被弃用,因此您应该使用:

Rails.application.deprecators.debug = true

要解决此问题,您可以启用完整的调试信息。 (请参阅帮助)

ActiveSupport::Deprecation.debug = true

正如 @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:

Rails.application.deprecators.debug = true

To solve this you could enable the full debugging information. (see the help)

ActiveSupport::Deprecation.debug = true

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. before Bundler.require(:default, Rails.env) if defined?(Bundler) in application.rb).

You can add a condition, like if ENV["DEBUG"] or if environment == :test to leave this in your config.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文