# Issues with database/object caching and testing

**URL:** <https://www.ruby-forum.com/t/issues-with-database-object-caching-and-testing/163188>\
**Category:** Rails\
**Created:** [March 18, 2009, 4:48am UTC](https://www.ruby-forum.com/t/issues-with-database-object-caching-and-testing/163188 "2009-03-18T04:48:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickybby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rickybby](https://www.ruby-forum.com/u/rickybby)\
**Post date:** [March 18, 2009, 4:48am UTC](https://www.ruby-forum.com/t/issues-with-database-object-caching-and-testing/163188/1 "2009-03-18T04:48:55Z")

</div>

Hi all,

I just burned two hours of development time trying to debug this issue.  
Can someone please tell me if there is a way to disable database caching  
when running unit tests or another way to avoid running into this type  
of problem??

This does not work:  
assert periods(:period1).ongoing? # Asserts true  
plays(:end\_of\_first\_half).process # Ends the period  
assert !periods(:period1).ongoing? # Asserts false

This DOES work:  
assert periods(:period1).ongoing? # Asserts true  
plays(:end\_of\_first\_half).process # Ends the period  
assert !Period.find(periods(:period1).id).ongoing? # Asserts true

I’ve only just now run into this issue after dozens of tests. Am I  
missing something obvious about expected behavior?

---

<div class="post-metadata">

**Author:** ![rickybby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rickybby](https://www.ruby-forum.com/u/rickybby)\
**Post date:** [March 18, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/issues-with-database-object-caching-and-testing/163188/2 "2009-03-18T19:10:43Z")

</div>

Ricky B. wrote:

> Hi all,
> 
> I just burned two hours of development time trying to debug this issue.  
> Can someone please tell me if there is a way to disable database caching  
> when running unit tests or another way to avoid running into this type  
> of problem??
> 
> This does not work:  
> assert periods(:period1).ongoing? # Asserts true  
> plays(:end\_of\_first\_half).process # Ends the period  
> assert !periods(:period1).ongoing? # Asserts false
> 
> This DOES work:  
> assert periods(:period1).ongoing? # Asserts true  
> plays(:end\_of\_first\_half).process # Ends the period  
> assert !Period.find(periods(:period1).id).ongoing? # Asserts true
> 
> I’ve only just now run into this issue after dozens of tests. Am I  
> missing something obvious about expected behavior?

I have run into this same problem, but don’t understand it either. To be  
safe, I always do a find on the object first, but wish I had a more  
reliable solution to this as well.

I too have wasted lots of debugging time only to find it was just an  
issue with my test code!

---

<div class="post-metadata">

**Author:** ![rickybby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rickybby](https://www.ruby-forum.com/u/rickybby)\
**Post date:** [March 18, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/issues-with-database-object-caching-and-testing/163188/3 "2009-03-18T19:40:08Z")

</div>

On Mar 18, 3:48 am, Ricky B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> assert !periods(:period1).ongoing? # Asserts false
> 
> ```
> 
> This DOES work:  
> assert periods(:period1).ongoing? # Asserts true  
> plays(:end\_of\_first\_half).process # Ends the period  
> assert !Period.find(periods(:period1).id).ongoing? # Asserts true

I don’t know exactly what this code is doing, but activerecord does  
cache associations and so on (and fixtures are only loaded once per  
test when you do periods(:period1). There’s no easy way to disable  
this, periods(:period1).reload.ongoing? is a little less ugly than  
other alternatives.

Fred
