Transactional fixtures in Rails: a head-banger
Amy and I were banging our heads today over some model unit tests we were running in Rails. Why on earth did the following updates not show up in the database after the test suite ran? The assertions all behaved as expected.
item.postal_code = "02138" assert item.valid? assert item.save
I mean, consarn it, we saved the record! Shouldn’t it be in the database?
Well, the answer turned out to be fairly simple. ActiveRecord’s fixtures, will, by default, rollback everything that was done on the database during the method in question, unless you set
self.use_transactional_fixtures = false
in your test class. I had strongly been under the impression that the fixtures were loaded once at the beginning of each method within a test class.
Now that we know that the logic is actually behaving as expected, we are back to the default behavior (i.e., using transactional fixtures.)
Reference here.
