Rails如何在Seeds.rb中使用FactoryGirl? [英] Rails How to use FactoryGirl in Seeds.rb?
问题描述
我想让Seeds.rb文件在Rails.root.join('spec')
目录中的文件中运行一个方法,该方法将用FactoryGirl生成的数据填充数据库.让我们使用"seed_data"方法将此文件称为"helpers.rb"
I want to have the Seeds.rb file run a method in a file from the Rails.root.join('spec')
directory which will fill the database with data generated by FactoryGirl. Lets call this file "helpers.rb" with the method "seed_data"
require Rails.root.join('spec','helpers.rb')
链接文件. 源
require Rails.root.join('spec','helpers.rb')
links the file. source
我想在before(:all) do seed_data end
中使用此方法来播种测试数据库,而在rake db:seed
中使用它来进行开发/生产数据库,效果相同.
This method I want to use in a before(:all) do seed_data end
to seed the test database and rake db:seed
for development/production databases with the same effect.
推荐答案
Seeds.rb
require Rails.root.join('spec','helpers.rb')
require 'rubygems' #so it can load gems
require 'factory_girl_rails' #so it can run in development
seed_data
spec/helpers.rb
def seed_data
Factory.create(:user)
end
这篇关于Rails如何在Seeds.rb中使用FactoryGirl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!