工厂女孩:创建一个与现有对象关联的对象
我想创建两个对象来引用我在 before(:each) 块中创建的另一个单个对象,
例如。
# in my factories.rb file..
factory :blah_1 do
association :foo, :factory => :foo
end
...
# in my spec..
before(:each) do
foo = Factory(:foo)
end
...
foo.blahs << Factory(:blah_1)
foo.blahs << Factory(:blah_1)
# some test on foo to make sure the right thing happened
当我运行此规范时,它尝试为 blah_1 和 blah_2 创建 foo 的实例,但失败了,因为我不允许重复的属性。
我希望让 blah_1 和 blah_2 都引用一个 foo 工厂。
预先感谢您的帮助:)
I'd like to create two objects that refer to another, single object that I've created in the before(:each) block
eg.
# in my factories.rb file..
factory :blah_1 do
association :foo, :factory => :foo
end
...
# in my spec..
before(:each) do
foo = Factory(:foo)
end
...
foo.blahs << Factory(:blah_1)
foo.blahs << Factory(:blah_1)
# some test on foo to make sure the right thing happened
When I run this spec it tries to create an instance of foo for both blah_1 and blah_2, failing because I don't allow a duplicate attribute.
I'd like to get both blah_1 and blah_2 to reference a single foo factory.
Thanks in advance for your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终所做的事情解决了我的问题(请告诉我这是否是不好的形式!)..
从我的角度来看,这似乎相当合理..
What I ended up doing that fixed me (please let me know if this is bad form!)..
It seems pretty reasonable from my perspective..