Validating Rails Templates

Now that you have the W3C checker installed on your own server, you can feed it URLs that aren’t open to the public. But, if you are developing an application in Ruby on Rails, you can go one step better than this, integrating the W3C validator into your automated testing.
In order to do this, you need to install the html_test plugin for Rails. Go into your Rails application’s root directory, and type:

script/plugin install

With this plugin in place, you now can use three new assertions in your functional and integration tests: assert_w3c returns true if the W3C validator approves of your HTML;
assert_tidy returns true if you’re using the HTML Tidy library, described below; and, assert_validates calls both of these. So, if you have a FAQ page you want to check with an integration test, you can write something like this:

def test_faq
get '/faq'
assert_response :success

If the HTML for this page is approved by the W3C validator, everything is fine. If this page is not valid, you will get quite a bit of output, which you should redirect to a file. This file will contain not only the results of your tests, but also the same HTML output that you would have gotten from the public, Web-based W3C validator. This means you’ll get a complete and easy-to-read description of what you did wrong. You’ll often discover that a large number of validation errors can be fixed with a small number of corrections. For example, when I ran this test against a sloppy FAQ page, I got six validation errors. I was able to fix all of them by indicating the appropriate namespace in my tag and removing an extraneous

from the end of the file.

Checking HTML validity in this way is nice and easy. (It can be time consuming, however, to invoke the validator on every single page; I think the trade-off is worthwhile, but you might disagree.) If you always want to check HTML validity, you can change your test environment’s configuration somewhat, so that it’ll happen automatically, without having to invoke assert_w3c each time.

To do this, you need to modify test_helper.rb, which sits at the top of the test directory, and which is included into every test program. All you have to do is add:

ApplicationController.validate_all = true
ApplicationController.validators = [:w3c]

You also can check the validity of URLs and redirects; although these aren’t checking HTML validity per se, they do come with the html_test plugin and are quite useful:

ApplicationController.check_urls = true
ApplicationController.check_redirects = true

With these four lines in your test_helper.rb, you can run your integration tests once again. If any of the validation tests fail, you can look at /tmp/w3c_last_response.html, which will contain the complete output of that failure. This doesn’t help very much if you have multiple failures, however. If you have designed your templates using the DRY (don’t repeat yourself) principle, fixing HTML markup problems shouldn’t be too bad. In many cases, you will need to change only one tag in the layout to fix everything.

Source of Information : Linux Journal Issue 182 June 2009


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner