在railstutorial,爲什麼筆者選擇使用這個(清單10.25): http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersRailstutorial:DB:填充與工廠女孩
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
User.create!(:name => "Example User",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar")
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(:name => name,
:email => email,
:password => password,
:password_confirmation => password)
end
end
end
來填充假的用戶數據庫,並且還(上市7.16) http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end
看來,這兩種方式在數據庫中正確(不工廠女孩在數據庫中創建用戶)創建的用戶?創建測試用戶的兩種不同方式的原因是什麼?它們有什麼不同?一種方法何時比另一種更適合?
嗯......但爲什麼他不會在rake任務中使用FactoryGirl,而不是'User.create!'?這不可能嗎? – Ajedi32 2012-08-14 20:35:08