Cucumber and Factory Girl

Cucumber is a test suite for Rails (and PHP). Firstly in <application>/config/environments/test.rb add the lines:

config.gem "rspec", :lib => false, :version => ">=1.2.2"
config.gem "rspec-rails", :lib => false, :version => ">=1.2.2"
config.gem "webrat", :lib => false, :version => ">=0.4.3"
config.gem "cucumber", :lib => false, :version => ">=0.3.0"
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => ""

Then when you do:

$ rake db:test:clone

all the above gems will be installed

To run:

$ cucumber features

All code is in <application>/features
E.g. under features: in file manage_posts.feature you will find all the Scenario declarations
Under <application>/features/step_definitions
you will find say file post_steps.rb (for the post model). This will look like:
When /^I delete the (\d+)(?:st|nd|rd|th) post$/ do |pos|
  visit posts_url
  within("table > tr:nth-child(#{pos.to_i+1})") do
    click_link "Destroy"

Then /^I should see the following posts:$/ do |posts|
  posts.rows.each_with_index do |row, i|
    row.each_with_index do |cell, j|
      response.should have_selector("table > tr:nth-child(#{i+2}) > td:nth-child(#{j+1})") { |td|
        td.inner_text.should == cell

Given /^I have the following post records$/ do |table|
  table.hashes.each do |post_hash|
  Factory(:post, post_hash)
  # table is  Cucumber::st::Table

Note that up till now we have NOT used factory girl. To use this (assuming the gem above has installed):

You need to create a directory under /test called factories (note plural). Then create a file called post.rb which looks like:

Factory.define :post do |p|
    p.title "Title of Post"
    p.body "Body of Post"
Specifiying steps

For each type of test (each posts) you will need a post_step.rb file (in /features/step_definitions/). Then you will have steps like:

Given /^I add ([\d]+) published posts$/ do |number| 
    number.to_i.times {Factory(:published_post)} 

To just run steps for posts
$ cucumber features -t @posts
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License