简体   繁体   中英

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? 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. For example a after(:create) won't be called if you use MyObject.new .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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