Cucumber Table transformations with Factory Girl

Problem
After reading this post here, to be able to use the Cucumber table trasformatios feature to build objects in your cucumber tests, you would like to use FactoryGirl instead of the standard model.
The reason for using FactoryGirl could be that your model needs a few more attributes (mandatory fields), that you don’t want to specify in your cucumber table, but you want the factory to take care of them.

Solution
The only difference would take place in the transformation step.
So if you would originally have the following:

Transform /^table:Vehicle,User,Start,End$/ do |table|
  table.hashes.map do |hash|
    vehicle = Vehicle.create!({:regno => hash['Vehicle']})
    user = User.create!({:first_name => hash['User']})
    booking = Booking.create!({:start_at => hash['Start'],
                               :end_at => hash['End']})
    {:vehicle => vehicle, :user => user, :booking => booking}
  end
end

and for example your user has also fields like password, date of birth, that are mandatory in the model validations, you would put them in your factory define declaration like:

Factory.define(:user) do |u|
u.first_name "Name"
u.last_name "Lastname"
u.address1 "Address1"
u.address2 "Line 2"
u.address3 "Line 3"
u.zip "1111"
u.city "City"
u.sequence(:email) {|n|"teste#{n}@test.com"}
u.profile_picture File.open(File.join(Rails.root,"features/fixtures/user.png"))
u.password "password"
end

and replace the tranformation to:

 

Transform /^table:Vehicle,User,Start,End$/ do |table|
table.hashes.map do |hash|
vehicle = Factory(:vehicle, :regno => hash['Vehicle'])
user = Factory(:user,:first_name => hash['User'])
booking = Factory(:booking, :start_at => hash['Start'],
:end_at => hash['End'])

{:vehicle => vehicle, :user => user, :booking => booking}
end
end

The actual step would similar to the one in the blog article:

Given /^the following bookings?:$/ do |table|
# table is a Cucumber::Ast::Table
table.each do |group|
booking = group[:booking]
associations = {:vehicle => group[:vehicle], :user => group[:user]}
booking.update_attributes(associations)
end
end

The single-table inheritance mechanism failed to locate the subclass:

Problem
You have correctly setup your Single Table Inheritance tables as in:

class Parent < ActieRecord::Base ; end class Child < Parent ; end

but everytime you are trying to use that in a cucumber feature, or in rails console you get the following error:

The single-table inheritance mechanism failed to locate the subclass: 'child'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Parent.inheritance_column to use another column for that information. (ActiveRecord::SubclassNotFound)

Solution
Make sure that when use the subclass name in a feature, or you create a record in the database the initial letter is in capital!
So make sure that in this case the type column contains 'Child' and not 'child'

undefined method `visit’ for # (NoMethodError)

Problem
When using Cucumber and Webrat in a ruby on rails 3 application you see the error: undefined method `visit' for # (NoMethodError) and your scenario fails.

Solution
Replace the line:
config.mode = :rails
with the line:
config.mode = :rack
in the Webrat.configure block, in your
app/features/support/env.rb
file.

Adding Webrat, Cucumber and RSpec to a new Rails 3 project

Problem

You would like to start using BDD in a new Ruby on Rails 3 application, and would like to install Webrat, Cucumber and RSpec to your project.

Solution

Follow the steps below (taken from the RSpec Book), for creating a new Rails 3 application and adding the necessary testing frameworks:

  1. Create your new ruby on rails application: rails new my_app
  2. Go to your new application directory: cd my_app
  3. Edit your Gemfile to include the following:
    group :development, :test do
    gem "rspec-rails", ">= 2.0.0"
    gem "cucumber-rails", ">= 0.3.2"
    gem "webrat", ">= 0.7.2"
    end
  4. Use bundler to install all the gems and dependencies: bundle install
  5. Install the rspec files: script/rails generate rspec:install
  6. Install the cucumber files: script/rails generate cucumber:install
  7. Run the following and you shouldn’t be seeing any errors:
    rake db:migrate
    rake db:test:prepare
    rake spec
    rake cucumber