Reputation: 1271
I gave generating test data a first shot by trying to populate my database with a simple script that creates a sufficient number of records for my models accounting for all dependencies (esp. polymorphism).
This is my seeds.rb
require 'factory_girl_rails'
50.times do
@user = FactoryGirl.create(:user)
FactoryGirl.create(:contact, :user => @user)
@question = FactoryGirl.create(:question, :user => @user)
FactoryGirl.create(:user_answer, :question => @question, :authorable => @user)
@contact = FactoryGirl.create(:contact, :user => @user)
FactoryGirl.create(:contact_answer, :question => @question, :authorable => @contact)
end
As an example, here ist the question
factory:
FactoryGirl.define do
factory :question do
title "What is the best place to travel in " + Random.country + "?"
body Random.paragraphs(2)
association :user, :method => :build
end
end
While the Random
class does produce one random term, that term remains the same for all instances created. In this case I would get 50 questions of, say, "What is the best place to travel in Spain?" and the identical two paragraphs of text for each.
What am I missing?
Upvotes: 5
Views: 1558