getting started with Cucumber and Webrat on Rails

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"
4. Start fixing failing tests
Cucumber will show you:
  • 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.
Webrat provides you a lot of useful steps to get started. You can see them in features/step_definitions. I encourage you to go in and check it out.
5. Create features/step_definitions/general_steps.rb and put this in:
Then /^I should see what is up$/ do
  save_and_open_page
end
6. Create features/step_definitions/blog_steps.rb and put this in:
Given /^there is a post titled "([^\"]*)" with content "([^\"]*)"$/ do |title, content|
  Post.new(:title => title, :content => content).save
end
Note: After modifying the database, you will need to run rake db:migrate on your dev environment and then rake db:test:prepare to clone the changes to the cucumber database.
Have fun cuking!
This entry was posted in open source, programming, web programming and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free