ruby FactoryGirl 中的 build 和 create 方法有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14098031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between the build and create methods in FactoryGirl?
提问by Avery
The Factory Girl introductiondelineates the difference between FactoryGirl.build()and FactoryGirl.create():
Factory Girl 介绍描述了FactoryGirl.build()和之间的区别FactoryGirl.create():
# Returns a User instance that's not saved
user = FactoryGirl.build(:user)
# Returns a saved User instance
user = FactoryGirl.create(:user)
I still doesn't understand the practical differences between the two. Can someone give an example where you would want to use one and not the other? Thanks!
我仍然不明白两者之间的实际差异。有人可以举一个例子,你想使用一个而不是另一个吗?谢谢!
回答by Helio Santos
The create()method persists the instance of the model while the build()method keeps it only on memory.
该create()方法build()保留模型的实例,而该方法仅将其保留在内存中。
Personally, I use the create()method only when persistence is really necessary since writing to DB makes testing time consuming.
就个人而言,我create()仅在确实需要持久性时才使用该方法,因为写入 DB 会使测试耗时。
e.g.
例如
I create users to authentication with create()because my authentication engine queries the DB.
我创建用户进行身份验证,create()因为我的身份验证引擎查询数据库。
To check if a model has an attribute the build()method will do because no DB access is required.
要检查模型是否具有属性,该build()方法将执行此操作,因为不需要访问数据库。
it{Factory.build(:user).should respond_to(:name)}
Update
更新
"There is one exception that build actually 'creates' when you are building associations, i.e your association are no longer in memory but persisted. Keep that in mind" – Shakes
“有一个例外,当您建立关联时,构建实际上是在‘创建’,即您的关联不再存在于内存中而是持久存在。请记住这一点” – Shakes
回答by chasm
Using FactoryGirl.build(:factory_name)does not persist to the db and does not call save!, so your Active Record validations will not run. This is much faster, but validations might be important.
UsingFactoryGirl.build(:factory_name)不会持续到 db 并且不会调用save!,因此您的 Active Record 验证将不会运行。这要快得多,但验证可能很重要。
Using FactoryGirl.create(:factory_name)will persist to the db and will call Active Record validations. This is obviously slower but can catch validation errors (if you care about them in your tests).
UsingFactoryGirl.create(:factory_name)将持续到数据库并调用 Active Record 验证。这显然较慢,但可以捕获验证错误(如果您在测试中关心它们)。
回答by Nesha Zoric
FactoryGirl.create()will create new object and associations (if the factory has any) for it. They will all be persisted in a database. Also, it will trigger both model and database validations.
Callbacks after(:build)and after(:create)will be called after the factory is saved. Also before(:create)will be called before the factory is saved.
FactoryGirl.create()将为它创建新的对象和关联(如果工厂有的话)。它们都将保存在数据库中。此外,它将触发模型和数据库验证。回调after(:build)和after(:create)将在工厂保存后调用。也before(:create)将在保存工厂之前调用。
FactoryGirl.build()won't save an object, but will still make requests to a database if the factory has associations. It will trigger validations only for associated objects.
Callback after(:build)will be called after the factory is built.
FactoryGirl.build()不会保存对象,但如果工厂有关联,仍会向数据库发出请求。它只会触发关联对象的验证。after(:build)工厂建好后会调用回调。
Note that in most cases when testing models are best to use build_stubbedfor better performance. Read more about it here.
请注意,在大多数情况下,测试模型时最好使用build_stubbed以获得更好的性能。在此处阅读更多相关信息。

