2011-08-11 10 views
0

RSpecとファクトリーガールを使用して、作成時に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 

は何とかこのなんとかですか私は違っこれにアプローチする必要がありますか?

代替工場を作成しないのはなぜ

答えて

0

これはあなたが何ができるかをされます。

2

:彼らはまだ挿入し、新しい協会に押し込みれた順に保存されます

@business = Factory(:business_one) 

# clear associations so it's in a known state 
@business.hours.clear 

@business.hours << Factory(:day_one) 
@business.hours << Factory(:day_two) 

Factory.define :business_with_altnernate_hours, :parent => :business_one do 
    after_create do |business| 
    business.hours.clear 
    Factory.create(:day_one, :business => business) 
    Factory.create(:day_two, :business => business) 
    end 
end 
関連する問題