Posted by Peter Donald Wed, 09 May 2007 05:17:00 GMT

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

You can grab it from subversion at;

svn checkout http://assert-valid-asset.googlecode.com/svn/trunk/

The original article describing the plugin is here .