Cucumber 101

I know it is pretty fucking cliched; however, it is hard to go through a Ruby day without some surious testing. That's right, if you don't know what "TATFT" means than you haven't been doing craftsmen level software development for all that long. In this post we are going to take a newly formed Rails application and get it up to speed with some simple Cucumber style tests. In addition, this will be my first software based post, which is kind of exciting...I think.

The first thing that needs to be done is cracking open that Gemfile and let's add in these lines:

group :test do
  gem 'cucumber-rails', :require => false
  gem 'capybara-webkit'
  gem 'database_cleaner'

If you are one of those people who feel the need to place version numbers in your Gemfile, go ahead. As an aside, I recommend NOT doing this. Use your Gemfile.lock and your version control du jour to keep gem versions inline with your dev and production platforms.

The three gems you added into your Rails application are as follows:

  1. Cucumber-rails adds all the requires into your app where needed. A rails g cucumber:install will also need to be run after bundle installing the above gems.
  2. Database cleaner will cleanse your test environment after each test.
  3. Capybara-webkit is a handy gem for running @javascript tagged tests in a headless environment. It does require you download/compile QT-Webkit; however, this is a breeze with Homebrew (it is listed as 'qt')

Once you finnish installing QT (Tested on 10.8.0 @2012.08.18 and working great) you should head over to /railsAppRoot/features/support/env.rb and add in this string:

Capybara.javascript_driver = :webkit

And VOILA! You are ready to start testing! For a handy way to run commands/tests, using command line Vim and Tmux, read this post by someone way smarter than myself. Keep your eyes peeled for another post on Cucumber stories and how to write reusable yet sensible tests in your Rails app.