Rails : Bdd setup

30 Sep 2013

This post will demonstrate how I setup rails for bdd. I mainly use 3 gems spork, rspec and cucumber.

Why Spork?

I use spork to run my tests faster. I experianced 2x to 5x faster test running time when I use spork. My Gemfile look similar to the below one.

group :development, :test do
  gem 'rspec-rails'
  gem 'spork', :git => 'git@github.com:sporkrb/spork.git'
  gem 'guard-spork'
  gem 'guard-rspec'
  gem 'guard-cucumber'
end

group :test do
  gem 'cucumber-rails',  :require => false
end

The Setup

So I added all the needed gems to my Gemfile and now its time to setup rspec and cucumber. Now setup rspec and cucumber with the below commands which will generate related files and directory structure.

rails g rspec:install
rails g cucumber:install

Now you need to setup spork for rspec and cucumber.

spork rspec --bootstrap

The above command will generate spork.prefork and spork.eachrun blocks in the spec/spec_helper.rb. Now you need to manually move the code generated by rails g rspec:install into the spork.prefork block.

spork cucumber --bootstrap

As you done for rspec, the above command will generate the spork.prefork and spork.eachrun blocks in the features/support/env.rb and you need to move the code generated by rails g cucumber:install into the spork.prefork block.

Now its time to setup guard, which will watch your application code and tests and run the tests when ever you update the code/tests.

guard init spork
guard init rspec
guard init cucumber

This above command will generate Guardfile and add configuration for spork, rspec and cucumber.

Then you need to add cli: '--drb' to rspec and cucmber guard configuration, to run them over the Spork DRb server.

An example configuration with cli option will look like,

guard 'cucumber', cli: '--drb' do
  watch(%r{^features/.+\.feature$})
  watch(%r{^features/support/.+$})          { 'features' }
  watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { 
    |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' 
  }
end

Thats it. My bdd setup for rails is done.

If you find my work helpful, You can buy me a coffee.