Rails 3 中的工厂女孩和nested_attributes
我有 2 个模型,一个接受另一个的属性,我正在尝试找到一种巧妙的方法来使用 Factory Girl 为两个模型设置数据。
Class Booking
has_many :booking_items
accepts_nested_attributes_for :booking_items
Class BookingItem
belong_to :booking
我的工厂
Factory.define :booking do |f|
f.bookdate Date.today+15.days
f.association :space
f.nights 2
f.currency "EUR"
f.booking_item_attributes Factory.build(:booking_item) # doesn't work
end
Factory.define :booking_item do |f|
f.association :room
f.bookdate Date.today
f.people 2
f.total_price 20
f.association :booking
end
booking_spec
require "spec_helper"
describe Booking do
before(:each) do
@booking = Factory.create(:booking)
end
it "should be valid" do
#needs children to be valid
@booking.should be_valid
end
end
我查看了 rdocs,但似乎找不到我要找的东西。
I have 2 models, one which accepts attributes for the other and I'm trying to find a clever way to use Factory girl to setup the data for both.
Class Booking
has_many :booking_items
accepts_nested_attributes_for :booking_items
Class BookingItem
belong_to :booking
my factory
Factory.define :booking do |f|
f.bookdate Date.today+15.days
f.association :space
f.nights 2
f.currency "EUR"
f.booking_item_attributes Factory.build(:booking_item) # doesn't work
end
Factory.define :booking_item do |f|
f.association :room
f.bookdate Date.today
f.people 2
f.total_price 20
f.association :booking
end
booking_spec
require "spec_helper"
describe Booking do
before(:each) do
@booking = Factory.create(:booking)
end
it "should be valid" do
#needs children to be valid
@booking.should be_valid
end
end
I looked around the rdocs but couldn't seem to find what I was looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,你想要这样做,但是使用更简洁的语法:
当然你可以像这样快捷方式:
并像这样使用:
在 active_factory 插件 具有类似的工厂定义,它看起来像这样:
不幸的是,我还没有实现与factory_girl 的集成。不过,如果您有兴趣,我们非常欢迎任何意见。
If I understood you correctly, you want to do this, but with terser syntax:
Of cause you can shortcut it like this:
And use like this:
In active_factory plugin with similar factory definitions it would look like this:
Unfortunately I haven't yet implemented integration with factory_girl. Though if you interested any input is very welcome.