A couple of months ago I started using Cucumber in one of my projects. I think it’s a great tool for large projects. It makes it easier for developers joining the project and it gives the development team a lot of confidence when changing the code.
If you have a running set of tests, you know that you can go in, write a failing test, modify the code, add a feature and then run the tests again. If the tests fail, you broke something that was working: fix it. If you didn’t, great news: you have a new working feature!
There is a great Railscasts that explains how to get started: http://railscasts.com/episodes/155-beginning-with-cucumber
Here is a set of steps to follow.
1. Create your Rails app
rails blog
2. Add Cucumber and Webrat to your environment.rb
config.gem 'cucumber', :lib => false,
:version => '=0.4.3'
config.gem 'webrat', :lib => false,
:version => '>=0.5.3'
config.gem 'cucumber-rails', :lib => false,
:version => '>= 0.3.1'
3. Install your gems
cd blog
sudo rake gems:install
4. Generate your Cucumber code
cd blog
./script/generate cucumber --webrat
3. Start writing feature files
For example:
vi features/list_posts.feature
Feature: Listing all the posts in the blog
As a user
I want to see all the posts in the blog
Scenario: I go to the posts list page and I see all the posts
Given there is a post titled "Awesome post" with content "My cool content"
When I go to the posts page
Then I should see what is up
And I should see "Awesome post"
And I should see "My cool content"
And I should not see "New post"
- Passing steps
- Skipped steps: These steps weren’t evaluated due to failing or undefined step
- Failing steps: These steps don’t pass. Something isn’t working as it should
- Undefined steps: These steps aren’t defined in features/step_definitions/*.rb – You should define it or make sure you spelled this step correctly.
Then /^I should see what is up$/ do
save_and_open_page
end
Given /^there is a post titled "([^\"]*)" with content "([^\"]*)"$/ do |title, content|
Post.new(:title => title, :content => content).save
end
