Topic: Unit testing and associations

Hey folks,

Finally facing something I've been putting off for ages and trying to get into the habit of writing tests but I'm having a problem with one and I was hoping someone might be able to help me with. 

I've got a after_save filter on a a model which updates another model it belongs to (to keep a running total of a column's values) but this for some reason isn't tallying up when I test it.

My test currently creates a new parent object then two new child objects, associates them, then saves the parent (and so the children).  If I check the test.log I can see it all appear to work but when I try to do an assert_equal for it the value in the parent model always returns 0 and so fails the test.  I've also found that the counter cache exhibits the same behaviour.

When I test it manually all works fine.  Am I missing something!?

Cheers,

Jon

I can't stop thinking about Rails. Can't sleep, can't eat, can't concentrate on work. It must be love.

Re: Unit testing and associations

If you post the relevant code I'll take a look at it. If you've already figured it out though let us know what the solution was!

Re: Unit testing and associations

Hahaha, just as I was typing in the last line of this (long) post the penny dropped.

I had just forgotten to reload the object before the final assertion.

Have written tons more tests since this post so I think leaving it and coming back to it is what was needed.

Last edited by professionalnerd (2007-12-20 05:09:50)

I can't stop thinking about Rails. Can't sleep, can't eat, can't concentrate on work. It must be love.