【问题标题】:FactoryBot ActiveRecord::RecordInvalid: Validation failed: Year is requiredFactoryBot ActiveRecord::RecordInvalid:验证失败:需要年份
【发布时间】:2019-02-27 16:54:31
【问题描述】:

当我运行我的 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

【问题讨论】:

  • db 中有year 列的类型是什么?
  • 其中一个相关型号是否也需要一年(其工厂不提供)?
  • @smathy 你是对的。在我的关联模型中,我没有设置 year 的值。现在它的作品。谢谢你。请问你能把你的答案给接受它并赚取你的积分吗?。
  • 嘿,很酷,我试图以一种可能对碰巧遇到您的问题的其他人有所帮助的方式来制定我的答案。干杯。

标签: ruby-on-rails factory-bot


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多