(Rails) Assert_Select 烦人的警告

发布于 2024-08-10 02:44:57 字数 425 浏览 7 评论 0原文

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

谢谢

Does anyone know how to make assert_select not output all those nasty html warnings during a rake test? You know, like this stuff:

.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"

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

墨落画卷 2024-08-17 02:44:57

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

It's rather that your code is generating invalid HTML. I suggest running it through a validator and fixing all the validation errors.

梦回旧景 2024-08-17 02:44:57

您可以使用 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): .

You can find out which test ran into the problem by using the TESTOPTS v flag:
(bundle exec) rake test TESTOPTS="-v"

This will give:

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): .
提笔落墨 2024-08-17 02:44:57

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

Rails's HTML scanner expects XHTML, if you're using HTML4 where tags don't have explicit closing tags, you may get this warning... doesn't look like solved issue

眼泪都笑了 2024-08-17 02:44:57

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

What I'd want is to know where the warning is coming from. The fact it doesn't specify the test or the controller/action which generates the invalid HTML is the big problem for me.

世态炎凉 2024-08-17 02:44:57

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

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

I had some problems after an update to rails 3.0.9 and HAML 3.1.2
What I did was silence those ugly outputs with the following code in *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

Anyway, I don't recommend using a solution like this. Use only if you are in hurry, and give it a good comment to explain other developers why is that estrange piece of code there.

蒗幽 2024-08-17 02:44:57

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

追踪这个问题的最佳方法是仔细查看“打开周围的文本”和“关闭周围的文本”并尝试追踪打开发生在模板中的位置。

For me, the cause was valid but poorly formed HAML.

This is what I had:

%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

This is what is correct for what I'd wanted to do:

%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

The best way to track this down is to look very carefully at the "text around open" and "text around close" and try to track down where in your template the open occurs.

月下客 2024-08-17 02:44:57

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

Even if you do rake test TESTOPTS="-v" and the error appears to be coming from your view templates, DON'T forget to check the application layout html. This happened to me and it took a few minutes longer than I'd like to admit going back and forth between a couple index.html.erb files before I finally figured it out. Same goes for any rendered partials.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文