在railstutorial中,作者为什么选择使用这个(清单10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
User.create!(:name => "Example User",
:email => "example@railstutorial.org",
: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 "mhartl@example.com"
user.password "foobar"
user.password_confirmation "foobar"
end看起来这两种方法都在数据库中创建用户,对吗(工厂女孩是否在数据库中创建用户)?创建测试用户的两种不同方式的原因是什么,它们有什么不同?什么时候一种方法比另一种更合适?
发布于 2011-06-12 11:27:10
Faker和Factory Girl在这些示例中用于两个不同的目的。
使用Faker创建一个rake任务,以便轻松地填充数据库,通常是开发数据库。这让你可以浏览你的应用程序,里面有很多填充的、虚假的数据。
工厂定义使得测试编写起来很方便。例如,在RSpec测试中,您可以这样写:
before(:each) do
@user = Factory(:user)
end那么@user就可以在随后的测试中使用了。它会将这些更改写入测试数据库,但请记住,每次运行测试时都会清除这些更改。
https://stackoverflow.com/questions/6319595
复制相似问题