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