2016-12-16 92 views
0

我有一個名爲Beats的模型,其中包含名稱,狀態,組,所有者等字段。我正在使用R Spec編寫測試並使用Factory Girl進行嘲諷,我面臨的問題是運行規範,它給出了錯誤,因爲ActiveRecord :: RecordInvalid:驗證失敗:節拍類型不能爲空,我驗證是否存在所有字段形式,有一個BeatType值的下拉列表,它在運行規範時也被調用,將它包含在Beat Factory中的方式是什麼?工廠女孩對象有很多通過和存在驗證

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 
+0

您應該驗證''在模型BeatBeatType'代替'Beat'模型beat_type'。 – Thanh

回答

1

能否請您嘗試使用

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

非常感謝!像魅力一樣工作。 –