具有许多通过和存在验证的工厂女孩​​对象

Factory girl object with has many through and presence validation

我有一个名为 Beats 的模型,它由名称、状态、组、所有者等字段组成。我正在使用 R Spec 编写测试并使用 Factory Girl 进行模拟,我面临问题 运行 specs 它给出错误 ActiveRecord::RecordInvalid: Validation failed: Beat Types can't be blank,我正在验证所有表单字段的存在,有一个带有 BeatType 值的下拉列表,它也会在 运行 规范时被调用,将它包含在 Beat 工厂中的方法是什么?

class Beat < ActiveRecord::Base
has_many :beat_beat_types
has_many :beat_types, through: :beat_beat_types

validates :name,:status,:group,:owner,:beat_types presence: true, length: {maximum: 255}
end

class BeatType < ActiveRecord::Base
has_many :beat_beat_types
has_many :beats, through: :beat_beat_types
end

class BeatBeatType < ActiveRecord::Base
belongs_to :beat
belongs_to :beat_type
end


Factory_File of beat

FactoryGirl.define do
factory :beat do
  name
  status
  group
  owner
end
end

能否请您尝试使用

FactoryGirl.define do
  factory :beat do
  name
  status
  group
  owner
  beat_types { build_list :beat_type, 1 }
end