Getting started with Story Driven Development for Rails with Cucumber
I've been hearing about Story Driven Development (SDD) for a while but I haven't tried it out because I was under the impression that there was a huge amount to learn and setup before I could get going. I'm not sure if that used to be true, but I started using Cucumber yesterday and it was really easy.
Install and configure
You'll need to install a bunch of RubyGems.
sudo gem install nokogiri term-ansicolor treetop diff-lcs hpricot cucumber
Install Cucumber into your Rails app.
ruby script/generate cucumber
Install WebRAT. Unfortuantely this doesn't seem to be available as a RubyGem. If you're using Git then install this as a submodule instead. We're not so I clone the repository then svn add it.
git clone git://github.com/brynary/webrat.git vendor/plugin/webrat
Writing your first story
Stories have three components: a business value, the role of a person that uses the feature and some description of the feature.
In order to [do something with business value]
As [role]
Should [describe the feature]
An example might be the ability to order a pizza from the online ordering system of a pizza delivery company.
Feature: Order Pizza
In order to get some hot, tasty pizza
A hungry pizza lover
Should be able to order pizza
Next we need to define some scenarios. Scenarios are things that can happen during the story. Most pizza places aren't open 24 hours a day so two simple scenarios are (1) the pizza shop is closed, and (2) the pizza shop is open.
Scenario: The pizza shop is closed
Given the pizza shop is closed
And I am on the home page
And I click "Feed Me!"
Then I should see "Sorry, the shop is closed"
Scenario: The pizza shop is open
Given the pizza shop is open
And I am on the home page
And I click "Feed Me!"
Then I should see "Your pizza will be with you soon"
The above description should go in a file called something like features/order_pizza.feature where it is lovely and version controlled and safe.
So, we now have a story that describes how a feature should behave. How does that get turned into acceptance tests? Well, you could pass these descriptions off to your testing team, or you could turn them into part of your test suite.
Automated tests: better than cake
You might notice that when installing Cucumber you got the directory features/steps. That's where you tell your test suite how to understand your stories. There are already two files here: common_webrat.rb which gives your test suite a few funky things like the ability to click links and env.rb which does pretty much the same stuff as spec/spec_helper.rb except for Cucumber. You can ignore env.rb, but common_webrat.rb will provide a few examples of how to start writing story steps.
Create a new file, order_pizza_steps.rb. This is where you define the steps involved in ordering pizza. It's pretty much just regular expressions which match each line of a scenario to some Ruby code.
Given /the pizza shop is open/ do
PizzaShop.open = true
end
Given /the pizza shop is closed/ do
PizzaShop.open = false
end
And /I am on the home page/ do
visits "/"
end
That's all we need to do. The common WebRAT steps provide the necessary mapping for clicking buttons and checking for feedback.
Running your stories
This is pretty simple: run rake features. You should get some rather pretty coloured output, and if anything has gone wrong Cucumber is pretty good at suggesting ways to fix it.
Found this article useful?
If you enjoyed this article I'd appreciate recommendations at Working with Rails.
Related articles
Leave feedback...
Commenting is closed for this article.

Cheers for this. It’s making some kind of sense now. Though I think I’ll need some better programmers than me to write some decent steps.
Thanks for the writeup.
Is cucumber aimed primarily at testing controllers, or is it just as effective doing unit testing? What about using mock objects and such in the tests?
I guess I’m still not understanding whether or not this can function as a drop-in replacement for rspec or not.
@Robert Cucumber should only be used in place of the stories part of RSpec. You should still write examples for your models, controllers and views.
Stories can be used to drive writing examples by picking a failing or pending story and discussing what needs to happen to make it pass with your pair. As your discussion continues you should write an RSpec example, watch it fail, write just enough code to make it pass, then continue your discussion. When you’re at the end of your discussion you have a complete feature and a bunch of examples that exercise it at a unit level.
Couldn’t get it to work as described.
First:
script/generate cucumber created a subdirectory inside features called step_definitions, but nothing called steps.
Second:
Even after copying and pasting your examples into a directory which I created manually and called steps, “rake features” fails (actually bombs with a trace). It only works when giving the features file explicitly:
rake features/order_pizza.features
@Yitzhak How odd. I’m using cucumber 0.1.8 – what version do you run?
Thanks for the useful article! In the webrat installation step, I think it needs to be ‘vendor/plugins’, not ‘vendor/plugin’. Also, for git n00bs like me, it would be useful to give the command to install the submodule: ‘git submodule add git://github.com/brynary/webrat.git vendor/plugins/webrat’ (it’s simple if you know how).