testing - Rails model method rspec test -
i have following test:
it 'should return array of other cars @ garage (excluding itself)' g1 = factorygirl.create(:garage)
c1 = factorygirl.create(:car) c1.garage = g1 c2 = factorygirl.create(:car) c2.name = "ford 1" c2.garage = g1 c3 = factorygirl.create(:car) c3.name = "vw 1" c3.garage = g1 expect(c1.other_cars_at_garage).to eq([c2, c3])
end
which should test method on model:
def other_cars_at_garage garage.cars.where.not(id: self.id) end
when run test, fails returns: #<activerecord::associationrelation []>
how should test pass? how check ar::associationrelation?
you not saving association between c1/c2/c3 , g1
c1 = factorygirl.create(:car) c1.garage = g1 c1.save #this saves g1 in c1's garage c2 = factorygirl.create(:car) c2.name = "ford 1" c2.garage = g1 c2.save c3 = factorygirl.create(:car) c3.name = "vw 1" c3.garage = g1 c3.save
in addition, it's best use
expect(c1.other_cars_at_garage).to match_array([c2, c3])
notice match_array
instead of eq
, means order of array unimportant, eq means order of array important
Comments
Post a Comment