Topic: Save method not saving changes in DB

This is probably a newbie question, but I noticed that when I run my unit tests, code that use the model save method do not save the changes to the test database. Hence, subsequent assert tests dependent on the change fails. Does anyone know why this is happening? Thanks.

JiggyMe
"Videos that Matter to You" powered by Ruby on Rails

Re: Save method not saving changes in DB

Well, I found my problem with the save method, it wasn't saving afterall and there wasn't a check for nil afterwards. I switched to using save! instead which will throw an exception if save fails. However, I still don't see the following code saving changes to the test database

assert u.save!

It didn't throw an exception meaning that save worked, but I still don't see the changes reflected in the test database, anyone knows why? The only DB changes that seem to take are the records defined the fixture file.

JiggyMe
"Videos that Matter to You" powered by Ruby on Rails

Re: Save method not saving changes in DB

When you say

I still don't see the changes reflected in the test database

, how exactly are you determining this?  The database essentially gets wiped after every test, so you can't just query the database after the test has completed.  Query the database inside of the unit test, and you can verify that the data has been 'written'.

BrewControl.com - Brewery and Brewpub management powered by RoR

Re: Save method not saving changes in DB

Thanks, that explains it. I was puzzled by the records left in the test database after all the unit tests completed. Those records are the ones specified in the fixture file but without any of the changes made during the unit tests execution.

JiggyMe
"Videos that Matter to You" powered by Ruby on Rails

Re: Save method not saving changes in DB

I've been wondering the same thing, except that I've got the unit test paused in the debugger (mid-test) and a psql session open to the test database. Shouldn't database updates be visible to my psql session?

Is there a transaction wrapping the whole test that would hide DB changes from another client session?

(There's no test data in my dev or production databases, so I know it's not a configuration goof.)

ETA: I found the use_transactional_fixtures setting, so that would explain it.

Last edited by korinthe (2010-06-02 11:56:03)