5

I am using Cucumber, RSpec, and Factory Girl for the testing of my Rails application. But I have several lookup tables that contain mostly static data. So I'm trying to figure out the best way to populate these when testing. Doing them individually in FactoryGirl seems tedious and I'd like to stay away from fixtures. For development and production, I populate them in my seeds.rb file.

Thanks!

Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
Adam Albrecht
  • 6,680
  • 4
  • 31
  • 35

2 Answers2

8

Use Factory Girl .sequence, Populator and Faker and you'll never run out of lab rats!

Factory.define(:model) do |m|
  m.sequence(:title)  { |n| "model-#{n}" }
  m.author            Faker::Name.name
  m.short             Populator.words(5)
  m.long              Populator.paragraphs(1..3)
end

Then maybe in a before :each block

@models = []
15.times { @models << Factory.create(:model) }

Or you can use only Populator to fill your database before tests.

Mirko
  • 5,207
  • 2
  • 37
  • 33
  • 1
    Syntax for FactoryGirl changed a bit with new updates: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md – Mirko Aug 31 '11 at 10:49
0

Maybe something like

rake RAILS_ENV=test db:seed

in your test helper file?

David Sulc
  • 25,946
  • 3
  • 52
  • 54