我的模特;家庭has_many:邻居和邻居belongs_to:家庭
每个家庭必须有一个邻居:
validates :neighbors, presence: {message: 'You must enter at least one neighbor']
我正在尝试创建一个工厂
factory :household, class: "Household" do
household_name "Brooke"
neighbor
end
factory :neighbor, class: "Neighbor" do
first_name "Tom"
last_name "Brooke"
end
it "has a valid factory" do
household = create(:household, neighbors: :neighbor)
expect(household).to be_valid
end
这给了我:
undefined method `neighbor=' for #<Household:0x007fd45ec85138>
如何设置Factory Girl以反映协会?
答案 0 :(得分:1)
我终于这样做了:
我把邻居带出了家庭工厂:
factory :household, class: "Household" do
household_name "Brooke"
end
在我的测试中,我在测试中创建了协会:
it "has a valid factory" do
neighbor = create(:neighbor)
household = create(:household, neighbors: [neighbor])
expect(household).to be_valid
end