Factory Girl step definitions for Cucumber

Did you know… Factory Girl includes some steps for your integration testing pleasure? They are currently available but remain relatively unknown.

Read the source at lib/factory_girl/step_definitions.rb. They make Direct Model Access a little easier from your Cucumber features.

Example usage

Define your factories normally in test/factories.rb or spec/factories.rb:

Factory.define :user do |user|
  user.email                 { Factory.next(:email) }
  user.password              { "password" }
  user.password_confirmation { "password" }

Factory.define :author, :parent => :user do |author|
  author.after(:create) { |a| Factory(:article, :author => a) }

Factory.define :recruiter, :parent => :user do |recruiter|
  recruiter.is_recruiter { true }

Make sure Factory Girl is available in your config/environments/cucumber.rb:

config.gem 'factory_girl', :version => '>= 1.2.3'

Require Factory Girl’s step definitions in features/support/env.rb:

require 'factory_girl/step_definitions'

Then, write Cucumber features using the simple “create record” step:

Given a user exists

… or the “create record & set one attribute” step:

Given an author exists with an email of "author@example.com"

… or the “create record & set multiple attributes” step:

Given the following recruiter exists:
  | email            | phone number | employer name |
  | bill@example.com | 1234567890   | thoughtbot    |

Avoid boilerplate

These steps will be available for all your factories, so stop writing boilerplate steps and shake what Factory Girl gave you.

Pair with one of our expert developers to level up your skills with Coaching by thoughtbot. Save time learning best practices and techniques for reducing technical debt in Ember, Ruby, Haskell, and Go in 1-on-1 sessions tailored to your goals.