When transactional fixtures just won't work (rSpec)
Sat Feb 19 00:00:00 -0500 2011
I had a issue with rSpec some days ago that made me loose a lot of time. When running the suite using “rake spec”, and just after the first example when objects were being created and persisted in the test database, the second and all subsequent examples would fail because of validation errors caused by an unclean test database.
The first configuration line I looked into was the “config.use_transactional_fixtures”, but it was correctly set to true.
After hours trying to catch the issue I finally tracked it down to a “self.class.commit_db_transaction” line of code on a model. I won’t get into why I used this line of code, but enough to say that it wreaks havoc with rSpec’s transactional examples.
A simple solution is to just add a condition such as:
“self.class.commit_db_transaction unless Rails.env.test?”
Beware that this might cause issues with how your code behaves, and it probably won’t work the way you wanted to. Anyway, if you are forcing the commit of the transaction, you might also want to try to find a better way to solve you original problem instead of using it.