2013-05-12 74 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