【问题标题】:(Rails) Assert_Select's Annoying Warnings(Rails) Assert_Select 的恼人警告
【发布时间】:2010-12-12 21:00:35
【问题描述】:

有谁知道如何使 assert_select 在 rake 测试期间不输出所有那些讨厌的 html 警告?你知道的,像这样的东西:

.ignoring attempt to close body with div
opened at byte 1036, line 5
closed at byte 5342, line 42
attributes at open: {"class"=>"inner02"}
text around open: "</script>\r\t</head>\r\t<body class=\"inner02"
text around close: "\t</div>\r\t\t\t</div>\r\t\t</div>\r\t</body>\r</ht"

谢谢

【问题讨论】:

  • 我遇到了同样的问题,W3C 验证器告诉我我的代码是有效的。我的问题是关于 &lt;==&gt; 我在视图中的一些 javascript (&lt;script&gt; if (something &gt;= 1) {return 1;} &lt;/script&gt;) 中使用。解决方案是用 CDATA 标记 (&lt;script&gt;//&lt;![CDATA[ if (something &gt;= 1) {return 1;} //]]&gt;&lt;/script&gt;) 围绕脚本。有关详细信息,请参阅此 SO 答案:stackoverflow.com/questions/66837/…
  • 关闭你的标签:)

标签: ruby-on-rails functional-testing


【解决方案1】:

您的代码正在生成无效的 HTML。我建议通过验证器运行它并修复所有验证错误。

【讨论】:

  • 例如,您可以在此处粘贴原始 HTML 并获取错误列表。 validator.w3.org/#validate_by_input 在我的例子中,

    标签中有一个

    标签,但如果没有验证器工具,我永远不会发现它。
  • 这条线打破了我的var html = '&lt;option value="' + data.id + '""&gt;' + data.name + '&lt;/option&gt;';注意'附近的双“”。容易错过
【解决方案2】:

您可以使用 TESTOPTS v 标志找出问题所在的测试: (bundle exec) rake test TESTOPTS="-v"

这将给出:

test: Request the homepage should have a node list. (PresentControllerTest): .
test: Request the homepage should have the correct title. (PresentControllerTest): ignoring attempt to close div with body
  opened at byte 4378, line 89
  closed at byte 17745, line 393
  attributes at open: {"class"=>"colleft"}
  text around open: "class=\"colmid\"> \n\t\t\t<div class=\"colleft\""
  text around close: "x2.js\" ></script>\n  </body>\n</html>\n\n"
ignoring attempt to close div with html
  opened at byte 4378, line 89
  closed at byte 17753, line 394
  attributes at open: {"class"=>"colleft"}
  text around open: "class=\"colmid\"> \n\t\t\t<div class=\"colleft\""
  text around close: "</script>\n  </body>\n</html>\n\n"
.
test: Request the homepage should not set the flash. (PresentControllerTest): .
test: Request the homepage should respond with 200. (PresentControllerTest): .

【讨论】:

    【解决方案3】:

    Rails 的 HTML 扫描器需要 XHTML,如果您使用 HTML4,其中标签没有明确的结束标签,您可能会收到此警告...看起来不像已解决的问题

    【讨论】:

      【解决方案4】:

      我想要知道警告来自哪里。它没有指定生成无效 HTML 的测试或控制器/动作这一事实对我来说是个大问题。

      【讨论】:

      • 是的,这很烦人!我花了一段时间才最终摆脱所有警告错误。
      【解决方案5】:

      在更新 rails 3.0.9 和 HAML 3.1.2 后我遇到了一些问题 我所做的是使用 *test_helper.rb* 中的以下代码使那些丑陋的输出静音

      # Wrap up the method assert_select because after updating to Rails 3.0.9 and HAML 3.1.2,
      # I don't know why but it was raising warnings like this:
      #     ignoring attempt to close section with body
      #     opened at byte 6157, line 128
      #     closed at byte 16614, line 391
      #     attributes at open: {"class"=>"left-column"}
      #     text around open: "->\n\n\n</span>\n</div>\n<section class='left"
      #     text around close: "'1'>\n</noscript>\n</body>\n</html>\n"
      # But the HTML seems to be valid (in this aspects) using a HTML validator.
      ActionDispatch::Assertions::SelectorAssertions.class_eval do
        alias_method :assert_select_original, :assert_select
        def assert_select(*args, &block)
          original_verbosity = $-v # store original output value
          $-v = nil # set to nil
          assert_select_original(*args, &block)
          $-v = original_verbosity # and restore after execute assert_select
        end
      end
      

      无论如何,我不建议使用这样的解决方案。仅在您赶时间时使用,并给出一个很好的评论来解释其他开发人员为什么那里有那段陌生的代码。

      【讨论】:

        【解决方案6】:

        对我来说,原因是正确的,但 HAML 格式不正确。

        这就是我所拥有的:

        %ul
          %li
            = link_to "Help", help_url
          - if current_user
            %li
              = link_to "Users", users_url
              = link_to "Logout", logout_url
          - else
              = link_to "Login", login_url
        

        这对于我想做的事情是正确的:

        %ul
          %li
            = link_to "Help", help_url
          %li
            - if current_user
              = link_to "Users", users_url
              = link_to "Logout", logout_url
            - else
              = link_to "Login", login_url
        

        跟踪此问题的最佳方法是非常仔细地查看“打开的文本”和“关闭的文本”,并尝试跟踪在模板中打开的位置。

        【讨论】:

          【解决方案7】:

          即使您执行rake test TESTOPTS="-v" 并且错误似乎来自您的视图模板,不要忘记检查应用程序布局 html。这发生在我身上,在我最终弄明白之前,我承认在几个 index.html.erb 文件之间来回切换花了几分钟的时间。任何渲染的部分也是如此。

          【讨论】:

            猜你喜欢
            相关资源
            最近更新 更多
            热门标签