0

在 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
4

1 回答 1

1

尝试这样的事情:

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

    cart
    order
    product   
  end 
end
于 2013-05-12T23:26:54.713 回答