这些模型之间的关系是
airport HAS_MANY flights
flight HAS_MANY flight_skus
如何使用FactoryGirl轻松创建具有父级和祖父级的flight_sku
before(:all) do
@airports = AIRPORTS.collect{|airport| FG.create(:airport, code: airport)}
@flight = FG.create(:flight, from_airport: @airports[0], to_airport: @airports[3])
@sku = FG.create(:flight_sku, flight: @flight)
@flight_2 = FG.create(:flight, from_airport: @airports[1], to_airport: @airports[2])
@sku_2 = FG.create(:flight_sku, flight_id: @flight_2.id)
end
NoMethodError:
undefined method `single_room=' for #<Room:0x007fcbed2e3d60>
Did you mean? singleton_method
factory :create_rooms_list do
single_room
twin_room
end
factory :single_room do
name "單人房"
guests 1
end
factory :twin_room do
name "雙人房"
guests 2
end
答案 0 :(得分:2)
factory :flight_sku do
other_fields
flight
end
factory :flight do
other_fields
airport
end
factory :airport do
other_fields
end
通过致电build(:flight_sku)
,您可以创建一个flight
,其中包含airport
和一个新的flight_sku
,并设置了所述关联。
通过致电create(:flight_sku)
,您将创建所有这些。
通过调用build_stubbed(:flight_sku)
,您将创建flight_sku
和两个关联的存根资源。