与 Factory Girl 工厂更新记录
我使用 RSpec 和 Factory Girl 创建一条记录,该记录在创建时会在 after_create 中自动创建关联的“小时”记录。
但我想用非默认时间进行测试,最好是我在女工厂中定义的时间。这就是我想我想做的:
before (:all) do
@business = Factory(:business_one)
# when that business is saved, a set of default hours is automatically saved as well
# how would I now update the hours with fixtures?
# so, ideally something like:
@business.hours[0] << Factory(:day_one)
@business.hours[1] << Factory(:day_two)
...etc...
end
这是否可行,或者我是否需要以不同的方式处理这个问题?
Using RSpec and Factory Girl, I create a record that when created, has associated 'hours' records automatically created in after_create.
But I want to test with the non-default hours, preferably ones I define in factory girl factories. Here's what I'm thinking I'd like to do:
before (:all) do
@business = Factory(:business_one)
# when that business is saved, a set of default hours is automatically saved as well
# how would I now update the hours with fixtures?
# so, ideally something like:
@business.hours[0] << Factory(:day_one)
@business.hours[1] << Factory(:day_two)
...etc...
end
Is this doable somehow or do I need to approach this differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不创建一个替代工厂:
Why not create an alternate factory:
您可以这样做:
它们仍将按照您推入新关联的顺序插入和保存。
This is what you can do:
They will still be inserted and saved in the order in which you push in the new association.