Fixing XML testing with assert_select
The Rails 1.2.x tree has quickly become indispensable to me. RESTful routes with respond_to to serve multiple content types makes API development a snap. Meanwhile assert_select provides incredibly flexible view testing. assert_select is all about CSS-style selectors for validating markup, so it’s only natural to want to use it on XML. Overall it works pretty well, but perhaps you’ve seen warnings like:
ignoring attempt to close property with area
opened at byte 2608, line 66
closed at byte 2872, line 73
attributes at open: {}
text around open: "ip>\n </property>\n <property>\n <acre"
text around close: ">SF City Limits SW-S</area>\n <bathroo"
These warnings are essentially harmless, but they are very irritating, especially when thousands of them scroll by every time you run your tests. Why do they occur? Because assert_select is parsing the content as HTML, and HTML has several tags that are automatically closed:
- img
- br
- hr
- link
- meta
- area
- base
- basefont
- col
- frame
- input
- isindex
- param
As it turns out, Rails’ HTML scanner has the ability to parse as XHTML which mostly solves these warnings. Jamis outlines has outlined a user-level solution, but I didn’t find that unti l had already analyzed and come up with my own solution to the problem.
I just submitted a patch that automatically sets the parsing method based on the contents of the ContentType (ie. response.content_type). Hopefully this patch will be accepted and we won’t have to hack around the issue anymore.
Leave Your Mark