Andres23Ramirez
Andres23Ramirez

Reputation: 657

Clean test database, only of the data generated running the tests with RSPEC and Capybara

I have an application in rails 4.1 to which I am doing the test with RSPEC and Capybara, for her I have a test database with data that I use to perform the tests, I am running a test that generates some data that are stored in the bd, or what I need is that after running the tests these data are not stored in it.

I have used the gem database_cleaner (clean test database, only of the data generated running tests with RSPEC and Capybara), but this clears all the data of the test bd and I only want to erase the data that are generated by the tests.

I have this configuration, and the test is a javascrpit test:

config.use_transactional_fixtures = false
    config.before(:suite) do
        if config.use_transactional_fixtures?
             raise(<<-MSG)
             Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
             (or set it to false) to prevent uncommitted transactions being used in
             JavaScript-dependent specs.

             During testing, the app-under-test that the browser driver connects to
        uses a different database connection to the database connection used by
            the spec. The app's database connection would not be able to access
            uncommitted transaction data setup over the spec's database connection.
            MSG
        end
    DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
    DatabaseCleaner.strategy = :transaction
end

    config.before(:each, type: :feature) do
        driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

        if !driver_shares_db_connection_with_specs
            DatabaseCleaner.strategy = :truncation
    end
end

Upvotes: 2

Views: 1655

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49870

If you're creating records in the tables that contain the data you want saved in your tests, then there's no easy way (simple configuration change of database_cleaner, etc) to do what you want - maintain data existing at the beginning of the test. That's because that would be the transactional mode for database_cleaner which isn't compatible with Capybara and JS test in rails 4.1 (in Rails 5.1 transactional mode is compatible and database_cleaner is no longer needed for most configurations)

If you're not creating/deleting/updating records in the tables you want saved from your tests then you can tell database_cleaner to skip those tables.

Since it's not easy to do what you want you should ask yourself exactly why you're trying to do something most other people aren't and maybe adjust the way you're doing things.

If you still want to continue with what you're trying to do (and you aren't modifying any of the existing data in your tests (just adding new rows) then you'll need to create a before hook that stores the maximum id in each table, and then an after hook that removes records with larger ids, and not use database_cleaner

Upvotes: 2

Chetan Datta
Chetan Datta

Reputation: 445

You can use this

rake db:reset RAILS_ENV=test

Upvotes: 1

Related Questions