我有 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
预订规格
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,但似乎找不到我要找的东西。