1
votes

My Model; Household has_many :neighbors and Neighbor belongs_to :household

Each Household must have a neighbor:

   validates :neighbors, presence: {message: 'You must enter at least one neighbor']

I am trying to create a factory

   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

This gives me:

  undefined method `neighbor=' for #<Household:0x007fd45ec85138>

How do I set up Factory Girl to reflect the Association?

1
Another assignment !!!Rohit Gupta

1 Answers

1
votes

I finally did this:

I took neighbor out of the household factory:

  factory :household, class:  "Household" do
      household_name "Brooke"
    end

And for my test I created the Association within the test:

   it "has a valid factory"  do
      neighbor = create(:neighbor)
      household = create(:household, neighbors: [neighbor])
      expect(household).to be_valid
   end