William Notowidagdo Kiranatama Staff
Knowledge


Those who are doing BDD knows Cucumber. But Cucumber is not the only tool that helps you to do acceptance testing of Rails applications. There is Steak. Steak is pretty much like Cucumber but in plain Ruby. No givens, whens or thens. No steps, no English, just Ruby. In this post I will show you how to add Steak to an existing application and start to write acceptance test. The existing Rails application is a simple application that allow you manage your Recipes database with its CRUD functions. A recipe has name and description. The acceptance spec will cover following changes: I want to categorize my recipes e.g., Salad, Main Dish. Salad, for example, is category name. A recipe will belongs to a category.

Getting started

Add the gem to Gemfile
group :development, :test do
  gem 'steak'
end
..then run
bundle install
now let's really install Steak into your application
rails generate steak:install

Writing acceptance test

Start with the categories
rails generate steak:spec categories
Command above will make Steak generate spec/acceptance/categories_spec.rb, which looks like
require 'acceptance/acceptance_helper'
feature 'Categories', %q{
  In order to ...
  As a ...
  I want ...
} do

  scenario 'first scenario' do
    true.should == true
  end

end
First of all, let's change the description of the feature like this
feature 'Categories', %q{
  In order to categorize recipes
  As a administrator
  I want to create and manage categories
} do
..and write the scenario: An administrator should be able to create category and show all categories on the categories index page.
scenario 'Category index' do
  Category.create!(:name => 'Salad')
  Category.create!(:name => 'Main Dish')
  visit categories_path
  page.should have_content('Salad')
  page.should have_content('Main Dish')
end

Acceptance test before code

In BDD you
  1. write the acceptance test then
  2. coding the feature
Let's do that with Steak. We wrote the acceptance test. Now run it and watch it failed.
rspec spec/acceptance/categories_spec.rb
Now generate the categories scaffold, run the database migration and run the test again and watch it passed. On to the recipes, generate the acceptance test
rails generate steak:spec recipes
Write the scenario
scenario 'Recipe should belongs to category' do
  category = Category.create!(:name => 'Salad')
  recipe = Recipe.new
  recipe.name = 'Long Island Salad'
  recipe.description = 'Mix your favorite fruit and veggy'
  recipe.category = category
  recipe.save!

  recipe.category.name.should == 'Salad'
end
Run it
rspec spec/acceptance/recipes_spec.rb
..and watch it failed
Failure/Error: recipe.category = category
NoMethodError:
  undefined method `category' for #<Recipe:0x9c4b694>
Add category_id to table recipes and define the association in Recipe model
class Recipe < ActiveRecord::Base
  belongs_to :category
end
Run the spec again and you will see it passed.

Conclusion

Steak let you doing BDD faster since you don't have to write the feature file in English and continue to the step definition in Ruby. But actually Steak is incomparable to Cucumber though and you should pick your weapon of choice based on your needs. Me? since I don't enjor veggetables, I choose Steak.