Categories
computer

Rails 2.0: Testing For Well-Formed XML With assert_well_formed

Here’s an easy way to validate that you’re always rendering well-formed HTML in an ordinary Rails application. I’ve written and verified this on Rails 2.0.1…

First, define the custom assert_well_formed assertion in your test_helper.rb, like so…

require 'rexml/document'

class Test::Unit::TestCase

def assert_well_formed

assert_block "HTTP response was not well-formed XML." do

 

begin

 

REXML::Document.new(@response.body)
true

rescue Exception => e

 

false

 

end

 

end

 

end

end

Then, in your functional test case, use it like this…

def test_index

set_basic_authentication
get :index
assert_well_formed
assert_response :success

end

That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *