未创建依赖对象

时间:2017-06-13 05:57:19

标签: factory-bot

我有Sale和依赖的SaleItem类

class Sale
    has_many :sale_items
end

class SaleItem
    belongs_to :sale
end

我有两个工厂......

FactoryGirl.define do
    factory :sale_item do
        sale
    end
end

FactoryGirl.define do
    factory :sale do
        sequence(:name) {|n| "Name #{n}" }

        factory :sale_with_sale_items do
            after :build do |sale|
                build_list :sale_item, 5, :sale => sale
            end
        end
    end
end

然后我从测试函数创建对象为...

object = create :sale_with_sale_items
assert object.save, 'Error saving sale'

尽管密切关注原始文件,我仍然得到......

Minitest::UnexpectedError: ActiveRecord::RecordInvalid: Validation failed: Sale must have at least one sale item.
    test/models/sale_test.rb:14:in `block in <class:SaleTest>'
test/models/sale_test.rb:14:in `block in <class:SaleTest>'

关于这可能出错的任何想法?

0 个答案:

没有答案