Class Bookinghas_many :booking_itemsaccepts_nested_attributes_for :booking_itemsClass BookingItembelong_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 workendFactory.define :booking_item do |f| f.association :room f.bookdate Date.today f.people 2 f.total_price 20 f.association :bookingend
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 endend
我环顾了rdocs,但似乎无法找到我想要的东西.
解决方法 如果我理解正确,你想要这样做,但使用更简洁的语法:booking_item = Factory(:booking_item,:people => 4)booking = Factory(:booking,:booking_item => booking_item)
因为你可以像这样快捷方式:
def with_assocs factory,assocs_hashes = {},attrs = {} assoc_models = Hash[ assocs_hash.map { |k,v| [k,Factory(k,v)] } ] Factory factory,attrs.merge(assoc_models)end
并使用这样的:
@booking = with_assocs :booking,:booking_item => {:people => 3}@booking.should be_valID
在具有类似工厂定义的active_factory plugin中,它看起来像这样:
models { booking - booking_item(:people => 3) }booking.should be_valID
不幸的是,我还没有实现与factory_girl的集成.虽然如果你感兴趣任何意见,非常欢迎.
总结以上是内存溢出为你收集整理的ruby-on-rails – 在rails 3中的工厂女孩和nested_attributes全部内容,希望文章能够帮你解决ruby-on-rails – 在rails 3中的工厂女孩和nested_attributes所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)