【问题标题】:Ignoring attempts to close x with y忽略用 y 关闭 x 的尝试
【发布时间】:2012-07-06 16:55:37
【问题描述】:

Ruby on Rails 有时会给您带来烦人的“忽略用 y 关闭 x 的尝试”warnings arising from assert_select。这些警告通常是无效 HTML 的结果,但有时即使 HTML 有效,它们也会出现。我的错误在运行ruby test/functional/my_controller_test.rb 时看起来像这样:

..ignoring attempt to close div with h2
  opened at byte 8551, line 207
  closed at byte 9554, line 243
  attributes at open: {"class"=>"my_css_class", "id"=>"object_1"}
  text around open: "  \r\n  \r\n  \r\n  \r\n\r\n  <div class=\"my_css_class"
  text around close: "</a>\r\n      </h2>\r\n\r\n      <span"

但没有尝试关闭带有 h2 标签的 div。我尝试了一个 HTML 验证器,但没有成功。 The -W0 parameter mentioned by Giles seems to help - ruby -W0 test/functional/my_controller_test.rb 不再给出警告,但这不适用于 rake test:whatever。 -W0 有什么作用,如何避免使用它?

【问题讨论】:

    标签: html ruby-on-rails ruby testing


    【解决方案1】:

    在测试助手中:

    class ActionController::TestCase  
      include Devise::TestHelpers
      Paperclip.options[:log] = false
      Mocha::Deprecation.mode = :disabled
    
      #
      # kill verbsity
      #
      verbosity = $-v
      $-v = nil
    
    end
    

    【讨论】:

      【解决方案2】:

      有各种command line options for Ruby unit tests。 -W 不属于它们,它是纯 Ruby 的命令行选项。正如ruby --help 所说,ruby -W[level] 命令行选项设置 Ruby 的警告级别; 0=静音,1=中等,2=详细(默认)。 ruby -W0 将警告级别设置为静音。

      $ ruby --help
        [...]
        -w              turn warnings on for your script
        -W[level]       set warning level; 0=silence, 1=medium, 2=verbose (default)
      

      -W 标志还激活了 Ruby 的“详细”模式。 Mislav 对the verbose mode 有很好的解释。在 Ruby 代码中,可以使用 $VERBOSE 全局变量的值设置和测试详细程度,该变量可以有 3 种状态:nil ("0")、false ("1") 和 true ("2")。因此,您可以通过设置

      来抑制 Test::Unit 测试的警告
      $VERBOSE = nil 
      

      在您的test_helper.rb 中。要运行 RSpecs 测试,您可以suppress ruby warning similarly

      【讨论】:

        猜你喜欢
        • 2013-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        • 1970-01-01
        • 2014-09-02
        相关资源
        最近更新 更多