Testing controllers for DB changes?


#1

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


#2

On Nov 16, 9:43 pm, davetron5000 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


#3

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

foo = Foo.find(foo.id)

simply use
foo.reload