pseudofish

powered by text files

programming

RSpec XHTML Validation of Views

A great way to learn is to read other people’s code. And in that vein, Chris Lowe has a great post on suggested OSS Rails apps to look into.

One technique I picked up from [simply_agile][] is to validate the HTML output by your views. That way, bad HTML isn’t introduced.

To start with, you need to install the [assert_valid_xhtml][] plugin and the libxml-ruby gem.

In your [spec/spec_helper.rb][] file, add the following inside the config block:

config.include ValidateXhtml

And at the end of the file, outside the config block, include the following:

describe "a standard view", :shared => true do
  it "should be successful" do
    response.should be_success
  end
  it "should be valid" do
    response.should be_valid_xhtml
  end
end

Now in your view specs, include the following line:

it_should_behave_like "a standard view"

And that’s it. Now each view will be tested for valid xhtml when you run your specs.

There are a lot of other great techniques scattered through the projects mentioned in the original post. And if you want more details on good practices for RSpec, check out the RSpec Book.