Home Contact Sitemap

Preston Lee

Founder, CEO, OpenRain.com

About

During the day I run OpenRain in Phoenix: a Ruby on Rails development shop. During the night... well... I still have to run OpenRain, but when I find time to breathe, I exhale here. If you'd like to get in touch professionally, don't hesitate to contact me via the OpenRain website.

Peace,

Preston

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!

Share/Save/Bookmark

Tags: , , , , ,

. 22 Dec 07 | Computer


Leave a Reply