(Rails)断言_选择恼人的警告

有没有人知道如何在rake测试期间使assert_select不输出所有那些讨厌的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: "\r\t\r\t<body class=\"inner02" text around close: "\t
\r\t\t\t
\r\t\t
\r\t\r</ht"

谢谢

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

你可以通过使用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
\n \n\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
\n \n\n\n" . test: Request the homepage should not set the flash. (PresentControllerTest): . test: Request the homepage should respond with 200. (PresentControllerTest): .

Rails的HTML扫描程序需要XHTML,如果你使用HTML4,其中标签没有明确的结束标签,你可能会得到这个警告…看起来不像是解决了问题

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

在更新到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\n\n
\n\n\n\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

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

对我而言,原因是有效的,但形成不良的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 

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

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

Interesting Posts