开发者

(Rails) Assert_Select's Annoying Warnings

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


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


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): .


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

  • http://dev.rubyonrails.org/ticket/1937
  • http://gilesbowkett.blogspot.com/2009/10/ignoring-attempt-to-close-foo-with-bar.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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜