Testing XML output in rspec and rails
Posted by glenn on Monday, December 10, 2007
Posted in rspec, testing, xml, rubyonrails
require File.dirname(__FILE__) + '/../../spec_helper'
describe "When viewing an election category as XML" do
include ModelHelpers
before(:each) do
@election = mock_model(Election, setup_election_helper)
assigns[:election] = @election
render 'election/show.xml.builder'
end
it "should export valid XML for inclusing in flash charts" do
response.should have_tag("chart[caption='Test Election'][xAxisName='Nominee'][yAxisName='Votes']")
response.should have_tag("chart set[value=100][label='nominee 1']")
response.should have_tag("chart set[value=200][label='nominee 2']")
response.should have_tag("chart set[value=200][label='nominee 3']")
response.should have_tag("chart set[value=200][label='nominee 4']")
response.should have_tag("chart set[value=200][label='nominee 5']")
end
end
I'd dabbled with using hpricot to parse the output and traverse through the tree to ensure the elements are all where they should be, but this meets the requirements perfectly. However, it may be worth wrapping the hpricot methods in customer rspec matchers in the long term to make the assertions more readable. For the specific situation though (generating XML to meet the spec of a 3rd party app) I'm happy enough with the result.
Glenn Gillen is a ruby and ruby on rails developer with clients in London, New York, Los Angeles, and Australia. Ruby Pond specialise in social networking development, and social and online marketing. Contact Ruby Pond if you wish to discuss hiring Glenn or one of our other developers for your own project.
blog comments powered by Disqus