Monday, February 11, 2008

Andrzej's Rails tips #5

Two things today, both related to RSpec stories: webrat, and using regexps in RSpec stories.

Webrat with RSpec stories

What is Webrat? "Webrat lets you quickly write robust and thorough acceptance tests for a Ruby web application". It uses Hpricot under the hood and is very easy to understand just by looking at the code.

It took me only 30 minutes to turn most of my tests in one of my applications from a classic IntegrationTest-based RSpec story to Webrat. Here is one example:

When "he creates an order" do
visits '/'
clicks_link "New order"
fills_in "Nr", :with => 'abc/2008'
fills_in "Company", :with => 'ABC company'
selects 'New'
clicks_button 'Create'

Thanks to Ben, for his great article describing RSpec stories with Webrat.

RSpec, response.should have_text

Sometimes, all you need is just a check whether there is a certain message visible on a page. One way of doing that is with regexps. Here is an example step that checks for the message:

Then "he sees a $message" do |message|
response.should have_text(

No comments: