我有一个 Divisions 模型和一个 Scorable 模型,它们的关联如下:
class Division < ActiveRecord::Base
has_one :scoring, :as => :scorable
validates :name, :presence => true
validates :scoring, :presence => true
end
class Scoring < ActiveRecord::Base
belongs_to :scorable, polymorphic: true
validates :scorable, :presence => true
end
如何测试这些模型的验证?如果这些工厂中的每一个都包含对彼此的引用,我会得到堆栈溢出。但是,如果我按以下方式制造工厂,我会在测试时收到 Scorable can't be blank 错误(显然):
FactoryGirl.define do
factory :division do
name "Division"
organisation
association :scoring, factory: :scoring
end
FactoryGirl.define do
factory :scoring do
first 12
second 10
third 8
scorable nil
end
end
如何从两个模型中测试关联的存在?