Sunday, December 19, 2010

Verifying Generated HTML With HAML, Cucumber, Capybara and RSpec

Several of the reports that the app that Joey and I are working on will be generated via HAML to be displayed in the app, for the users. The generated content will end up being HTML, but starts it's life as HAML markup - our preferred markup for Rails apps. It's easy to generate HAML into HTML, in code:

haml_file = "path/to/my/filename.haml"
haml_data = File.read(haml_file)
result = Haml::Engine.new(haml_data).render

We store the result of this in a model that is persisted in our database. Later, when a user requests the report, we pull the generated HTML out of the model and put it on the screen, so that it can be rendered in the user's browser.

All of this works quite well and is easy for us to do. However, we ran into a problem when trying to verify the content from a Cucumber feature. We used this to try and match the generated HTML content from our model, with the content that was shown on the page:

# Cucumber step definition file, using Capybara
Then /^I should see the annual physical results$/ do
 report = Report.for(assessment)
 page.should have_content report.data
end

Read more: LosTechies