Question

I'm writing a test case in Minitest that creates a database entry. After the test is run, all changes that the test did should be rolled back. What is a good way to achieve this?

require 'minitest/autorun'
require 'rubygems'
require 'sequel'
require 'factory_girl'

class TestPostgresqlFunctions < MiniTest::Unit::TestCase
  def test_simple_function
    Factory.find_definitions
    user = FactoryGirl.create(:user)
  end
end

With this code, the created entry will stay in the database. I'm not using Rails or any other framework. The database I'm using is PostgreSQL 9.1.

Was it helpful?

Solution

This should work (requires Sequel 3.29.0 or greater):

# Use this class as the base class for your tests
class SequelTestCase < MiniTest::Unit::TestCase
  def run(*args, &block)
    Sequel::Model.db.transaction(:rollback=>:always){super}
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top