FactoryBot ActiveRecord::RecordInvalid:验证失败:需要年份

FactoryBot ActiveRecord::RecordInvalid: Validation failed: Year is required

当我 运行 我的 rspec 测试时,我收到验证错误 year is required,但在我的工厂中我设置了年份值。我不知道为什么会收到此错误。

FactoryBot.define do
  factory :syllabus do
    year  { 2019 } 
    cycle_academic { "1" }
    modality { "presencial" }
    syllabus_content { "{}" }
    association :doc_template 
    association :user 
    association :curriculum_course
    association :cycle 
    credits { 4 }
    weekly_hours { 5 }

    trait :syllabus_estrategia do
      course { :estrategia }
    end

    trait :syllabus_ingles1 do
      course { :ingles1 }
    end

    trait :syllabus_ingles2 do
      course { :ingles2 }
    end

    trait :syllabus_matematica do
      course { :matematica }
    end    
  end
end

在我的 rspec 文件中,我用这行

调用了这个工厂
it "order to approver" do 
  syllabus_general =  FactoryBot.create(:syllabus) 
end

您显示的工厂中的 year 值没有任何问题,所以我猜测您在该工厂中拥有的关联模型之一也需要 year 和其他型号的默认工厂不提供它。

工厂总是在想其他事情正在发生什么。