Factory_girl 中有没有办法获取 attribute_for 并为同一实例元素创建?

发布于 2024-10-05 19:42:05 字数 87 浏览 6 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-10-12 19:42:05

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

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

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]
苍暮颜 2024-10-12 19:42:05

John Hinnegan 建议的解决方案是合理的,但您最好使用对象的 FactoryGirl.create 方法初始化,因为它通常会给你一个有效的对象。例如,如果您使用 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]

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]
萌酱 2024-10-12 19:42:05

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

@user_attributes = FactoryGirl.attributes_for(:super_user)

Thanks for this post, just wanted to add that the class is FactoryGirl

@user_attributes = FactoryGirl.attributes_for(:super_user)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文