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?
parent.connected_to_container?(other)
end
end
To this:
def connected_to?(other)
return false if parent.nil?
parent(true).connected_to_container?(other)
end
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
questions:
-
Should a model ever force a reload of its associations?
-
If a model does reload its associations how do you still use mocks?