[英]Is there a way in factory_girl to get attributes_for and create for the same instance element?
如果我想創建和實例使用“創建”構建策略然后想要使用“attributes_for”構建策略進行驗證,是否可以這樣做? 如果我在工廠使用序列? 機械師寶石有可能嗎?
我不完全明白。 而且我不是機械師的用戶。 但聽起來你只想做這樣的事情。
@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)
John Hinnegan建議的解決方案是合理的,但您最好使用FactoryGirl.create
方法進行對象初始化,因為它通常會為您提供有效的Object。 例如,如果使用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.