25

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?

Andrew Vit
  • 18,961
  • 6
  • 77
  • 84

3 Answers3

46

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.

ipd
  • 5,674
  • 3
  • 34
  • 49
eloyesp
  • 3,135
  • 1
  • 32
  • 47
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.

Eric Anderson
  • 3,692
  • 4
  • 31
  • 34
  • 1
    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 used – Nathan Aug 27 '16 at 18:23
  • You can you Module#prepend now. – Eric Anderson Aug 27 '16 at 20:06
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.

Shadwell
  • 34,314
  • 14
  • 94
  • 99
  • 1
    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`...) – Andrew Vit Sep 08 '10 at 22:39
  • 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! – Shadwell Sep 08 '10 at 22:47