Question

How can I set up dummy data with FactoryGirl?

Is there a way to set up dummy data with a factory in my integrated development environment?

Was it helpful?

Solution

I write a seed scribt to solve my problem:

in /db/seed.rb i implemented factory_girl and use a csv file to define custom datas.

seed.rb:

require 'factory_girl'
require 'csv'

CSV.foreach(Rails.root.join("datas.csv"), headers: true) do |row|
  post = FactoryGirl.create(:post) do |post|
    post.text = row[0]
  end
end

If you want to associate models with your test datas you can use FactoryGirl to create associated objects:

CSV.foreach(Rails.root.join("post_comments.csv"), headers: true) do |row|
  Post.all.each do |post|
    FactoryGirl.create(:comment, post: post) do |comment|
      comment.name = row[0]
    end
  end
end

You can also use the first CSV scribt and implement an factory wich create comments for each post.

FactoryGirl.create(:post_with_comments)

Now you can use FactoryGirl to create x records for testing datas with csv.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top