Rspec integration test does not clear the database

The database is not cleared after each integration test. The value remains in the database.

Is there a chance for this to happen?

thanks

+4
source share
6 answers
+11
source

Check out the tutorial here: http://railscasts.com/episodes/257-request-specs-and-capybara

It describes a database cleanup tool besides Rspec and Capybara

+2
source

You need a DatabaseCleaner, but you may find that the strategy :truncation too slow to work all the time. This is really only necessary for integration tests, so you can do this:

 # spec/spec_helper.rb require 'database_cleaner' config.before(:suite) do DatabaseCleaner.clean_with :truncation DatabaseCleaner.strategy = :transaction end config.before(:each) do |group| # The strategy needs to be set before we call DatabaseCleaner.start case group.example.metadata[:type] when :feature DatabaseCleaner.strategy = :truncation else DatabaseCleaner.strategy = :transaction end DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end # spec/features/your_feature_spec.rb require 'spec_helper' describe "An integration test", :type => :feature do end # spec/model/your_model_spec.rb require 'spec_helper' describe "A unit test" do end 

Obviously, this only applies if you are performing integration tests with RSpec directly, and not using Cucumber.

+2
source

How to prepare a test base for Rails rspec tests without using the rake specification?

My answer may interest you. This is a pleasant solution. For your case, you probably need something like

 config.after :each do ActiveRecord::Base.subclasses.each(&:delete_all) end 
+2
source

For those who use hooks before(:all) , keep in mind that these hooks are executed before the transaction associated with the device is opened. This means that any data created earlier (: all) is not intercepted using transaction lights . You can read the RSpec documentation .

I just wanted to mention this because I was passionate about it and my initial instinct was to go to Database Cleaner (which was not necessary and ultimately didn't work).

+1
source

There are two ways to do this:

  • Set up transaction examples for each individual test.
  • Set up transactional examples for all tests.

If you select option 1: at the top of the specification file, after:

 require 'spec_helper' 

Add

 RSpec.configure {|c| c.use_transactional_examples = true } 

This will result in transaction rollback after each example.

2. If you want to configure it globally, then in spec_helper.rb

 RSpec.configure do |config| ... config.use_transactional_examples = true # Add this ... end 
0
source

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


All Articles