ruby on rails - Testing Associations with rspec and Factory Girl -
my model; household has_many :neighbors , neighbor belongs_to :household
each household must have neighbor:
validates :neighbors, presence: {message: 'you must enter @ least 1 neighbor']
i trying create factory
factory :household, class: "household" household_name "brooke" neighbor end factory :neighbor, class: "neighbor" first_name "tom" last_name "brooke" end "has valid factory" household = create(:household, neighbors: :neighbor) expect(household).to be_valid end
this gives me:
undefined method `neighbor=' #<household:0x007fd45ec85138>
how set factory girl reflect association?
i did this:
i took neighbor out of household factory:
factory :household, class: "household" household_name "brooke" end
and test created association within test:
"has valid factory" neighbor = create(:neighbor) household = create(:household, neighbors: [neighbor]) expect(household).to be_valid end
Comments
Post a Comment