Database During Tests
Since every apps I’ve worked on that deals with a database is in Rails, I really didn’t have to think about transactional fixtures. I also use sqlite3 db in memory, so persistence was never an issue for me. Therefore, I admit that I completely forgot about the state of the test database since Rails’ default is to use transactional fixtures.
Here’s the deal, when you run tests outside of Rails, the fixtures are NOT transactional. Rspec will not clean up after itself by default. Take a look at below.
Before running this, I created a file named rtest.sqlite3 in the same directory.
Now, here’s the test I run:
This test passes without a hitch the first time, but when I run it again, it fails. Why? The record test added is still there. It does not roll back. In order to resolve this, you’ll have to make it transactional. To do this, I rewrite the test as follows. I’d like to thank Remi at remi.org for this.
In Rails, you can turn off transactional fixtures by setting config.use_transactional_fixtures to false, but I really don’t recommend that.