# Reloading Associations causes Tests to Fail

**URL:** <https://www.ruby-forum.com/t/reloading-associations-causes-tests-to-fail/127090>\
**Category:** Rails\
**Created:** [January 24, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/reloading-associations-causes-tests-to-fail/127090 "2008-01-24T22:37:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![startrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@startrader](https://www.ruby-forum.com/u/startrader)\
**Post date:** [January 24, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/reloading-associations-causes-tests-to-fail/127090/1 "2008-01-24T22:37:55Z")

</div>

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:

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?

---

<div class="post-metadata">

**Author:** ![startrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a6a055/32.png) [@startrader](https://www.ruby-forum.com/u/startrader)\
**Post date:** [January 24, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/reloading-associations-causes-tests-to-fail/127090/2 "2008-01-24T22:46:10Z")

</div>

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