ruby-on-railsunit-testingconfigurationdeprecatedshoulda

How can I mute Rails 3 deprecation warnings selectively?


I am upgrading a Rails 2 to Rails 3 application (code not written by me). The (well tested code) uses shoulda and Test::Unit, and extensively uses the macros should_create and should_change.

I understand from this discussion that the shoulda maintainers want to get rid of both methods but that people using Test::Unit don't find it necessary (not sure I am grasping the whole discussion though).

Anaway, is there a way to selectively turn of the deprecation warnings for specified macros? I already know from this posting that you can turn off the deprecation warnings in the Rake test output entirely by setting:

ActiveSupport::Deprecation.silenced = true

in your the test environment file and I also know that you can put specific pieces of code in a block to get them silenced:

ActiveSupport::Deprecation.silence do
# no warnings for any use of deprecated methods here
end

The latter is an option but would require me to go over all the tests and enclose the should_create macros in such a block. So I was wondering there was a way to eliminate warnings for specific macros entirely with one configuration setting?


Solution

  • In fact I stil had lots of other deprecation warnings from code that was in plugins or gems I had installed. In order to avoid most of that, I overwrote the Deprecation::warn method in test_helper.rb. So instead of the previous code, use:

    module ActiveSupport
      module Deprecation
        class << self
          def warn(message = nil, callstack = caller)
            # modif pvh the following lines make sure no deprecation warnings are sent 
            # for code that is
            # not by my but in some gem or plugin...
            return if silenced  || callstack.grep(/myrailsappname/).blank?
            # return if silenced 
            deprecation_message(callstack, message).tap do |m|
              behavior.each { |b| b.call(m, callstack) }
            end
          end
        end
      end
    end  
    

    BTW you need to replace myrailsappname with your app's name (the name of the folder it resides in). There is probably a more generic way to get that name, but I couldn't find it right now.