Reloading Associations causes Tests to Fail

I broke a test in an odd way today and it brought up some design and
testing questions. I was working through some problems in my
integration tests, and all was going well. I found that to get a certain
test to pass, I changed this

class Container
belongs_to :parent, :polymorphic=>true
def connected_to?(other)
return false if parent.nil?
To this:

def connected_to?(other)
return false if parent.nil?

I then made a few other changes, my integration test passed and I reran
the whole test suite. When I did, the unit test for Container which had
passed for quite a while, suddenly dumped Red tests all over the place.
I discovered that by reloading the association the Mock that I had so
carefully crafted to go in its place got replaced by what was actually
in the database – that is nil.

I ended up changing the way my integration tests work a little so I
could revert the change in Container, but it left me with some

  1. Should a model ever force a reload of its associations?

  2. If a model does reload its associations how do you still use mocks?

you need to expect the reload when you build the mocks. so when parent
is called it returns the mock.