16

有谁知道如何使 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"

谢谢

4

7 回答 7

13

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

于 2009-11-04T02:52:45.720 回答
10

您可以使用 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): .
于 2012-02-09T16:04:02.500 回答
4

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

于 2009-12-26T15:45:54.933 回答
4

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

于 2010-04-06T18:20:27.133 回答
2

在更新 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

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

于 2011-06-23T18:33:08.810 回答
0

对我来说,原因是有效的,但 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

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

于 2011-10-04T16:25:25.587 回答
0

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

于 2014-09-24T19:16:17.640 回答