Railsstutorial:db:populate vs. factory girl
||
在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
似乎这两种方式都可以在数据库中创建用户(工厂女孩在数据库中创建用户)吗?创建测试用户的两种不同方式的原因是什么,它们有何不同?一种方法何时比另一种方法更合适?
没有找到相关结果
已邀请:
1 个回复
届甸衬丝蚕
然后,@ user在随后的测试中可用。它将把这些更改写入测试数据库,但是请记住,每次运行测试时,这些更改都会被清除。