Trace source of deprecation warnings in rails tests
Asked Answered
D

4

26

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?

Drake answered 8/9, 2010 at 21:15 Comment(0)
T
47

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.


Update: as @Dorian mentions the method is deprecated on recent Rails versions, so you should use:

Rails.application.deprecators.debug = true
Tripper answered 10/1, 2012 at 2:7 Comment(1)
DEPRECATION WARNING: Calling debug= on ActiveSupport::Deprecation is deprecated and will be removed from Rails (use Rails.application.deprecators.debug= instead) (called from block in <main> at /app/config/environments/test.rb:34)Garrow
C
7

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.

Clayborne answered 29/1, 2011 at 22:19 Comment(2)
Looks like a reasonable solution. I'm chuckling to myself because with the release of Rails 5, alias_method_chain itself is deprecated. I guess it's fine to use alias_method_chain to find where alias_method_chain is being usedRhinencephalon
You can you Module#prepend now.Clayborne
C
1

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.

Carbonari answered 8/9, 2010 at 22:23 Comment(2)
I was hoping for a more general answer, not specifically for this error, but it makes sense: stepping in through the rails source was exactly what I ended up doing; it's probably the only way to do it. I added a breakpoint unless record.kind_of?(ActiveRecord::Base) above that line in actionpack so I could call where and get the full stack. (Turns out it was a content_tag_for...)Drake
Right, I see. I couldn't figure out a way of getting any more information other than stepping into the rails code and working back. You don't have to accept my answer if it doesn't answer what you wanted!Carbonari
G
0

The new way of doing this is:

Rails.application.deprecators.debug = true
Garrow answered 23/2 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.