【问题标题】:How can I mute Rails 3 deprecation warnings selectively?如何选择性地静音 Rails 3 弃用警告?
【发布时间】:2011-06-21 12:57:48
【问题描述】:

我正在将 Rails 2 应用程序升级到 Rails 3 应用程序(代码不是我编写的)。 (经过良好测试的代码)使用 shoulda 和 Test::Unit,并广泛使用宏 should_create 和 should_change。

我从this discussion 了解到,应该维护者希望摆脱这两种方法,但使用 Test::Unit 的人认为没有必要(虽然不确定我是否掌握了整个讨论)。

另外,有没有办法选择性地关闭指定宏的弃用警告?我已经从this posting 知道,您可以通过设置完全关闭 Rake 测试输出中的弃用警告:

ActiveSupport::Deprecation.silenced = true

在您的测试环境文件中,我也知道您可以将特定的代码块放在一个块中以使其静音:

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

后者是一个选项,但需要我检查所有测试并将 should_create 宏包含在这样的块中。所以我想知道有没有一种方法可以完全通过一个配置设置来消除特定宏的警告?

【问题讨论】:

    标签: ruby-on-rails unit-testing configuration deprecated shoulda


    【解决方案1】:

    我可以推荐一个替代品吗?

    module ActiveSupport
      class Deprecation
        module Reporting
          # Mute specific deprecation messages
          def warn(message = nil, callstack = nil)
            return if message.match(/Automatic updating of counter caches/)
    
            super
          end
        end
      end
    end
    

    【讨论】:

      【解决方案2】:

      老问题 - 但如果您有新的折旧,您想选择性地忽略:

      ActiveSupport::Deprecation.behavior = lambda do |msg, stack| 
        unless /LIBRARY_NAME/ =~ msg
          ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:stderr].call(msg,stack) # whichever handlers you want - this is the default
        end
      end
      

      这是给ActiveSupport 3的。

      【讨论】:

        【解决方案3】:

        事实上,在我安装的插件或 gem 中的代码中,我仍然收到许多其他弃用警告。为了避免大部分情况,我重写了 test_helper.rb 中的 Deprecation::warn 方法。所以代替之前的代码,使用:

        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  
        

        顺便说一句,您需要将 myrailsappname 替换为您的应用程序名称(它所在的文件夹的名称)。可能有更通用的方法来获取该名称,但我现在找不到。

        【讨论】:

          【解决方案4】:

          我想我找到了解决方案:在 test/test_helper.rb 中,我重新打开了模块并用相同的定义覆盖了宏定义,但弃用警告被注释掉了。不过,可能有更优雅的方法可以做到这一点......

          # modif pvh DEPREC
          # reopen the module and silence the deprecation statement to avoid 
          # having my results overflown by these deprecation warnings...
          module Shoulda # :nodoc:
            module Macros
              def should_create(class_name)
                ##::ActiveSupport::Deprecation.warn
                should_change_record_count_of(class_name, 1, 'create')
              end
            end
          end   
          

          【讨论】:

            猜你喜欢
            • 2011-02-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-15
            • 1970-01-01
            • 2013-12-20
            相关资源
            最近更新 更多