Validating your markup with RSpec and markup_validity
markup_validity was announced earlier today by Aaron Patterson on the comp.lang.ruby mailing list (read the full announcement here). Perfect timing, as this was something I have wanted to add to my test suites for some time now. Here are my installation notes:
sudo gem install markup_validity echo 'config.gem "markup_validity"' >> $RAILS_ROOT/config/environments/test.rb # Thats it!
I then added a basic spec for one of my controllers:
describe PackagesController do integrate_views it "should have an XHTML Strict compilant show page" do get :show, :id => 1 response.body.should be_xhtml_strict end end
However, I ran into an internal error when running the spec:
"Internal error: xmlSchemaVDocWalk, there is at least one entity reference in the node-tree currently being validated. Processing of entities with this XML Schema processor is not supported (yet). Please substitute entities before validation.."
My pages make use of HTML entities, and seemingly those aren't supported for
validation yet. Thus, I added the following method to my core_extensions.rb
file:
class String def strip_entities self.gsub(/&#?[A-Za-z0-9]+;/, '') end end
And changed the XHTML strict assertion in my spec to read like this:
response.body.strip_entities.should be_xhtml_strict. If you end up using
strip_entities a lot, I guess it could make sense to monkey patch.
All in all, this seems to be a nice gem for making sure your generated XHTML is valid.
UPDATE: This has been fixed by Aaron in HEAD, so the substitution tricky won't be necessary anymore after the new version is released.