在rails测试中跟踪弃用警告的来源

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

gems/actionpack-2.3.8/lib/action_controller/record_identifier.rb:76: warning: Object#id will be deprecated; use Object#object_id

不幸的是,这是显示的回溯的唯一一行,即使我使用rake test --trace运行它,并且log/test.log没有更多信息。

如何获取此警告的完整回溯或以其他方式确定我的代码中的哪一行导致它?

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

 ActiveSupport::Deprecation.debug = true 

正如@Eric Anderson所说,它应该放在Rails加载之后(即在application.rb中require 'rails/all'之后),但是在bundle运行之前捕获gem中的弃用警告(即在Bundler.require(:default, Rails.env) if defined?(Bundler)之前Bundler.require(:default, Rails.env) if defined?(Bundler)在application.rb中Bundler.require(:default, Rails.env) if defined?(Bundler) )。

您可以添加条件,例如if ENV["DEBUG"]if environment == :test ,将其保留在配置中。

有同样的问题。 一个gem引起了弃用警告,但我不知道哪个gem,因为Rail的消息只显示了我的代码中callstack的最后一位。 添加以下内容:

 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运行之前捕获gem中的弃用警告(即在Bundler.require(:default, Rails.env) if defined?(Bundler) in application.rb中)。

现在,当遇到弃用警告时,您将被丢弃在调试器中。 您可以将其保留(并使用if Rails.env.test? ),或者在找到问题if Rails.env.test?其删除。

当我在测试中得到这种警告时,通常是因为我使用的是模拟模型对象,并没有提供主动记录提供的所有方法。

一个很好的起点是rails代码本身。 查看引用的action_pack gem的源代码,导致错误的方法是dom_id 。 该方法为在页面上使用的对象生成id。 它似乎在内部的几个地方被调用(当然,除非你直接调用它!)但最可能的原因似乎是在一个对象上调用form_for