Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate lookup tables in Rails tests

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!

like image 343
Adam Albrecht Avatar asked Jan 22 '11 00:01

Adam Albrecht


1 Answers

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.

like image 62
Mirko Avatar answered Oct 20 '22 03:10

Mirko