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

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -