2013-05-12 72 views
0

如何設置依賴於FactoryGirl的依賴屬性?在FactoryGirl中設置父級的子屬性

FactoryGirl.define do 
    factory :line_item do 
    quantity 1 
    price 30 # I want price to come from the product association: self.product.price 

    cart 
    order 
    product 
    end 
end 

我試過,但沒有奏效:

factory :line_item do |f| 
    f.quantity 1 

    f.cart 
    f.order 
    f.product 
    after_build do |line_item| 
     line_item.price = line_item.product.price 
    end 
    end 

回答

1

嘗試是這樣的:

FactoryGirl.define do 
    factory :line_item do 
    quantity 1 
    price { product.price } 

    cart 
    order 
    product 
    end 
end 
+0

屬性的順序是否重要?在'價格'之後聲明'產品'屬性並不自然, – gamov 2013-11-26 08:58:42