Ever seen Database Cleaner in a project? “Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing”. I was playing around with the test helper and removed Database Cleaner to see what would happen. I noticed in a unit test that nothing is ever saved to the database. So what exactly does Database Cleaner clean? Let’s try it out.
Tests get rolled back
In a Rails 4.2 project with a user model and a basic test, create a user in the test. Now
rails c test to have a look at your test database and try
User.count. It should be 0 (make sure you don’t have a fixtures file).
Why are there no users after the test has run? Turns out that Rails wraps each test in a database transaction. Everything gets rolled back at the end. We can test this out by committing the transaction with a
User.connection.execute("COMMIT") at the end of the test. Now the
User.count in the rails test console will be 1.
Where does Database Cleaner come in?