在FactoryGirl中,构建和创建方法的区别是什么?

时间:2022-05-05 13:32:11

The Factory Girl introduction delineates the difference between FactoryGirl.build() and 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!

我仍然不理解这两者之间的实际差别。有人能举一个你想用一个而不是另一个的例子吗?谢谢!

3 个解决方案

#1


90  

The create() method persists the instance of the model while the 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()方法会这样做,因为不需要DB访问。

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

“在构建关联时,有一个例外是‘创建’,我。你的联想不再存在于记忆中,而是持续存在。记住这一点。

#2


10  

Using FactoryGirl.build(:factory_name) does not persist to the db and does not call save!, so your ActiveRecord validations will not run. This is much faster, but validations might be important.

使用FactoryGirl.build(:factory_name)不会持久化到db,也不会调用save!,因此您的ActiveRecord验证将不会运行。这要快得多,但是验证可能很重要。

Using FactoryGirl.create(:factory_name) will persist to the db and will call ActiveRecord validations. This is obviously slower but can catch validation errors (if you care about them in your tests).

使用FactoryGirl.create(:factory_name)将持续到db并调用ActiveRecord验证。这显然比较慢,但是可以捕获验证错误(如果您在测试中关心它们的话)。

#3


0  

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.

create()将为它创建新的对象和关联(如果工厂有的话)。它们都将被持久化到数据库中。此外,它还将触发模型和数据库验证。在工厂保存后调用(:build)和after(:create)之后的回调函数。在保存工厂之前(: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.

build()不会保存对象,但如果工厂有关联,仍然会向数据库发出请求。它只会触发关联对象的验证。(:build)之后的回调将在构建工厂之后调用。

Note that in most cases when testing models are best to use build_stubbed for better performance. Read more about it here.

注意,在大多数情况下,测试模型最好使用build_stub来获得更好的性能。在这里阅读更多。

#1


90  

The create() method persists the instance of the model while the 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()方法会这样做,因为不需要DB访问。

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

“在构建关联时,有一个例外是‘创建’,我。你的联想不再存在于记忆中,而是持续存在。记住这一点。

#2


10  

Using FactoryGirl.build(:factory_name) does not persist to the db and does not call save!, so your ActiveRecord validations will not run. This is much faster, but validations might be important.

使用FactoryGirl.build(:factory_name)不会持久化到db,也不会调用save!,因此您的ActiveRecord验证将不会运行。这要快得多,但是验证可能很重要。

Using FactoryGirl.create(:factory_name) will persist to the db and will call ActiveRecord validations. This is obviously slower but can catch validation errors (if you care about them in your tests).

使用FactoryGirl.create(:factory_name)将持续到db并调用ActiveRecord验证。这显然比较慢,但是可以捕获验证错误(如果您在测试中关心它们的话)。

#3


0  

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.

create()将为它创建新的对象和关联(如果工厂有的话)。它们都将被持久化到数据库中。此外,它还将触发模型和数据库验证。在工厂保存后调用(:build)和after(:create)之后的回调函数。在保存工厂之前(: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.

build()不会保存对象,但如果工厂有关联,仍然会向数据库发出请求。它只会触发关联对象的验证。(:build)之后的回调将在构建工厂之后调用。

Note that in most cases when testing models are best to use build_stubbed for better performance. Read more about it here.

注意,在大多数情况下,测试模型最好使用build_stub来获得更好的性能。在这里阅读更多。