Railsconf: In Praise of Non-fixtured Data – Kevin R. Barnes

May 6, 2009 Pivotal Labs

Fixtures Suck

When modeling complex business domains, not 3 model blog software, fixtures quickly become a quagmire. What’s the size of your domain? Kevin was working on a project with 180 models. This quickly became unworkable even with only 1 fixture file per model. Fixtures don’t scale well. Scenarios are also problematic as now you have to maintain a directory hierarchy of fixtures.

Use Data Generation instead

Factory Girl

# Define

Factory.define :user do |f|
  f.first_name 'John'
  f.last_name  'Doe'
end

# use

user = Factory(:user)

Object Daddy

Reopens your ActiveRecord class and adds generators for each attribute.

# define

class User << ActiveRecord::Base

  generator_for :username, :method => :next_user

  generator_for :email, :start => 'test@domain.com' do |prev|
    user, domain = prev.split('@')
    user.succ + '@' + domain
  end
end

# use

@user = User.generate!

Others

Machinist

Foundry

Fixjour

About the Author

Biography

Previous
Railsconf: Building a Mini-Google in Ruby – Ilya Grigorik
Railsconf: Building a Mini-Google in Ruby – Ilya Grigorik

Ilya's slides are already on the web. A few random notes: In 1994-1995 term frequency was state of the ar...

Next
Railsconf: Don't mock yourself out – Dave Chelimsky
Railsconf: Don't mock yourself out – Dave Chelimsky

Martin Fowler says Mocks Aren't Stubs and talks about Classical and Mockist Teting. Dave shows slightly am...