6

如果我想使用“create”构建策略创建和实例化,然后想使用“attributes_for”构建策略进行验证,是否可以这样做?如果我在工厂使用序列?机械师宝石有可能吗?

4

3 回答 3

11

不太确定我完全理解。而且我不是机械师的用户。但听起来你只是想做这样的事情。

@attributes = FactoryGirl.attributes_for(:my_object)
my_object = MyObject.create(@attributes)
my_object.some_property.should == @attributes[:some_property]
于 2011-03-06T03:04:47.473 回答
2

John Hinnegan建议的解决方案是合理的,但您最好使用该FactoryGirl.create方法进行对象初始化,因为它通常会给您一个有效的对象。例如,after(:create)如果您使用 a 将不会被调用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]
于 2015-02-07T14:20:29.783 回答
1

感谢这篇文章,只是想补充一下这个课程是 FactoryGirl

@user_attributes = FactoryGirl.attributes_for(:super_user)
于 2013-03-13T17:40:06.517 回答