Getting started with Steak
23 Jun 2011
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 Gemfilegroup :development, :test do gem 'steak' end..then run
bundle installnow let's really install Steak into your application
rails generate steak:install
Writing acceptance test
Start with the categoriesrails generate steak:spec categoriesCommand 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- write the acceptance test then
- coding the feature
rspec spec/acceptance/categories_spec.rbNow 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 recipesWrite 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' endRun 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 endRun the spec again and you will see it passed.
