Rollback changes created by minitest unit test

I am writing a test case in Minitest that creates an entry in a database. After starting the test, all changes that the test should perform should be discarded. 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 

Using this code, the created record will remain in the database. I do not use Rails or other frameworks. I am using a PostgreSQL 9.1 database.

+4
source share
1 answer

This should work (Sequel 3.29.0 or higher is required):

 # 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 
+1
source

Source: https://habr.com/ru/post/1383933/


All Articles