Enhanced Rails Plugin to Validate (X)HTML and CSS

It feels like forever ago that I was last working with ruby on rails. But I am back hacking away at a few rails and I slowly getting more familiarity with ruby itself rather than just using it as part of the rails platform.

Something I have wanted to do for a long time is automagically validate content such as (X)HTML, CSS, atom feeds etc that is generated by the web application. A while ago I put together the assert-valid-asset plugin that allowed you to assert (in functional tests) that the content generated is valid. However you still had to explicitly call the assert.

So recently I enhanced the plugin so that it can automatically validate generated content when configured to do so. To configure auto validation you need to set a class variable in Test::Unit::TestCase via code such as;

class Test::Unit::TestCase
self.auto\_validate = true
end

Then anytime content is generated in tests (such as via get and post methods) it will check the mime type of the content. If the content has a mime type of ‘text/html’ or ‘text/xhtml’ it will pass it to the ‘assert_valid_markup’ method. If the content is ‘text/css’ then it will be validated by the ‘assert_valid_css’ method.

Of course you may have tests that generate invalid (X)HTML or CSS (to work with specific unnamed browsers) and you may want to exclude these tests from the automatic content validation. This can be done by adding the test symbol to the exclude list or alternatively by adding the desired test symbols to an include list. Both of the following examples have identical behavior;

Example 1:

class FooControllerTest < Test::Unit::TestCase

self.auto\_validate\_excludes = \[:test\_foo, :test\_bar\]

def test\_foo;  ; end
def test\_bar;  ; end
def test\_baz;  ; end
end

Example 2:

class FooControllerTest < Test::Unit::TestCase

self.auto\_validate\_includes = \[:test\_baz\]

def test\_foo;  ; end
def test\_bar;  ; end
def test\_baz;  ; end
end

Update 12th of May, 2010

The plugin is now available on GitHub. See the GitHub project page