简体   繁体   English

在factory_girl中有没有办法获取attributes_for并为同一个实例元素创建?

[英]Is there a way in factory_girl to get attributes_for and create for the same instance element?

If I want to create and instance using "create" build strategy and then want to use "attributes_for" build strategy for verification, is it possible to do? 如果我想创建和实例使用“创建”构建策略然后想要使用“attributes_for”构建策略进行验证,是否可以这样做? And if I use sequences in the factory? 如果我在工厂使用序列? Is it possible in Machinist gem? 机械师宝石有可能吗?

Not quite sure I fully understand. 我不完全明白。 And I'm not a user of machinist. 而且我不是机械师的用户。 But it sounds like you just want to do something like this. 但听起来你只想做这样的事情。

@attributes = FactoryGirl.attributes_for(:my_object)
my_object = MyObject.create(@attributes)
my_object.some_property.should == @attributes[:some_property]

感谢这篇文章,只想添加该类是FactoryGirl

@user_attributes = FactoryGirl.attributes_for(:super_user)

The solution John Hinnegan suggest is sound, but you better use the FactoryGirl.create method for object initialization, because it will usually give you a valid Object. John Hinnegan建议的解决方案是合理的,但您最好使用FactoryGirl.create方法进行对象初始化,因为它通常会为您提供有效的Object。 For example a after(:create) won't be called if you use MyObject.new . 例如,如果使用MyObject.new则不会调用after(:create)

@attributes = FactoryGirl.attributes_for(:my_object)
my_object = FactoryGirl.create(:my_object, @attributes)
expect(my_object.some_property).to eq @attributes[:some_property]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM