FactoryGirl in Development Environment
Blog ยป FactoryGirl in Development Environment
Posted on 03 Dec 2010 01:15
You can use FactoryGirl to populate the databse during development. You need Factories for test. But I don't see any reason that you cannot use them in development environment also.
Lets say you have those factories
#test/factories/user.rb require 'factory_girl' unless Factory.factories[:user] Factory.define :admin, :class=> User do |u| u.sequence(:name ){|n| "admin#{n}"} u.sequence(:email_address ){|n| "admin#{n}@test.com"} u.password 'pass' u.administrator true end Factory.define :user do |u| u.sequence(:name ){|n| "user#{n}"} u.sequence(:email_address ){|n| "user#{n}@test.com"} u.password 'pass' u.administrator false end end
#test/factories/organzation.rb require 'factory_girl' unless Factory.factories[:organization] Factory.define :organization do |m| m.sequence(:name) {|n| "Test Organization #{n}"} end end
Then you can define such a rake task to populate your database
#lib/tasks/app_populate.rake namespace :app do desc 'Fill database with test data' task :populate => [:environment, "db:reset"] do require 'factory_girl' Dir['test/factories/*.rb'].each { |f| require f[0..-4] } p "Manifacturing Objects..." (1..2).each { Factory(:organization) } end end
Now go and type
rake app:populate
If you like this page, please spread the word:
You can contact me if you have questions or corrections.