# Testing controllers for DB changes?

**URL:** <https://www.ruby-forum.com/t/testing-controllers-for-db-changes/153419>\
**Category:** Rails\
**Created:** [November 16, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/testing-controllers-for-db-changes/153419 "2008-11-16T23:18:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davetron5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@davetron5000](https://www.ruby-forum.com/u/davetron5000)\
**Post date:** [November 16, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/testing-controllers-for-db-changes/153419/1 "2008-11-16T23:18:56Z")

</div>

New to Ruby and to Rails (but not to development or testing). Perhaps  
I’m going about this wrong, but I’m writing a functional test of my  
controller. The controller makes changes to the item being operated  
on and saves those changes to the database. My test code doesn’t  
detect these changes unless I reload from the database, e.g.:

def test\_something  
foo = foos(:my\_test\_foo)  
post :update, :id =\> foo.id, :bar =\> “Some Data”

assert\_equal(“Some Data”,foo.bar) # FAILS  
foo = Foo.find(foo.id)  
assert\_equal(“Some Data”,foo.bar) # PASSES  
end

I don’t necessarily have a problem with this, but it made me wonder if  
I’m approaching testing the wrong way. Am I?

Dave

---

<div class="post-metadata">

**Author:** ![davetron5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@davetron5000](https://www.ruby-forum.com/u/davetron5000)\
**Post date:** [November 16, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/testing-controllers-for-db-changes/153419/2 "2008-11-16T23:21:17Z")

</div>

On Nov 16, 9:43 pm, davetron5000 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> New to Ruby and to Rails (but not to development or testing). Perhaps  
> I’m going about this wrong, but I’m writing a functional test of my  
> controller. The controller makes changes to the item being operated  
> on and saves those changes to the database. My test code doesn’t  
> detect these changes unless I reload from the database, e.g.:

That’s the way it is. the controller will have been handling a  
different instance of Foo (that happens to correspond to the same row  
in the database). The instance in your test is not aware of what has  
been done to the other instance.

Fred

---

<div class="post-metadata">

**Author:** ![davetron5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a3d4f5/32.png) [@davetron5000](https://www.ruby-forum.com/u/davetron5000)\
**Post date:** [November 18, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/testing-controllers-for-db-changes/153419/3 "2008-11-18T17:05:05Z")

</div>

Fred is correct. A small suggestion to your code. Instead of

foo = Foo.find(foo.id)

simply use  
foo.reload
