New Factory Girl Definition Syntax

factory_girl and factory_girl_rails release candidates were released this week thanks to Joe and Josh.

The big change is some great-looking new syntax. Check it out:

Old:

Factory.sequence :email do |n|
  "email#{n}@example.com"
end

Factory.define :user do |factory|
  factory.name  { 'Ron Burgundy' }
  factory.email { Factory.next(:email) }
end

Factory.define :admin, parent: :user do |factory|
  factory.admin { true }
end

Factory.define :dog do |factory|
  factory.name { 'Baxter' }
  factory.association(:owner, factory: :user)
end

New:

FactoryGirl.define do
  sequence :email do |n|
    "email#{n}@example.com"
  end

  factory :user, aliases: [:owner] do
    name 'Ron Burgundy'
    email

    factory :admin do
      admin true
    end
  end

  factory :dog do
    name 'Baxter'
    owner
  end
end

It reminds me of the difference between Rails 2.x routes and Rails 3.x routes.

Read the GETTING STARTED document in the repo, then try it in your app:

gem 'factory_girl_rails', '1.1.rc1'
hound

Hound automatically reviews Ruby, JavaScript, CoffeeScript, and SCSS code in your GitHub pull requests and comments on style violations. It is free for open source repos and $12/month per private repo.