How can I clear my database between erroneous rspec specifications?

I added a rails application_cleaner gem to the database to clear my database between specifications. Here is my current configuration for database_cleaner located in spec/spec_helper.rb :

  config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) DatabaseCleaner.start DatabaseCleaner.clean end config.before(:each) do DatabaseCleaner.clean end config.after(:each) do DatabaseCleaner.clean end config.after(:suite) do DatabaseCleaner.clean end 

Now this configuration works fine until all recently launched specifications are skipped or not executed.

However, in case of an error (rspec does not give you a nice little E , such as minitest, it throws such a thing:

 09:17:32 - INFO - Running: spec /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/validations.rb:57:in `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid) 

), the database is not cleared! Residual data from the specification before the error remains in the database. I assume that this is because database_cleaner does not consider the erroneous specification complete and therefore does not clear the database.

Now it does no harm until you run your specs again. The residual data then leads to an error similar to this:

 09:17:32 - INFO - Running: spec /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/validations.rb:57:in `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid) 

The workaround for this error is quite simple; running rails_env=test rake db:reset or starting the database shell and releasing the corresponding tables using sql statements will clear this data and allow you to run the specifications without crashing.

However, this is annoying. One wrong character in any of my specifications (something to make it wrong, not crashing) makes the whole testing process wedge, almost like an automatic weapon firing mechanism!

What are your suggestions regarding database_cleaner? Do you have any example configurations that allow you to clean up the database even in the event of an error test?

I use guard to run my rspecs, which are further enhanced with factory -girl:

Gemfile:

 source 'https://rubygems.org' group :development do gem 'capistrano' gem 'rb-fsevent' gem 'debugger' end group :development, :test do gem 'rspec-rails', '~> 2.14.0' gem 'sqlite3' gem 'guard-rspec' gem 'guard-livereload', require: false gem 'guard-shell' gem 'webrick', '~> 1.3.1' end group :test do gem 'factory_girl_rails' gem 'capybara', '~> 2.2.0' gem 'selenium-webdriver' # capybara-webkit gem requires an application called 'libqtwebkit-dev' to build. To install 'libqtwebkit-dev' in Ubuntu, run # sudo apt-get install libqtwebkit-dev # gem 'capybara-webkit' gem 'rb-readline' gem 'launchy' gem 'database_cleaner' end group :production do gem 'pg' # gem 'puma' end # rails version gem 'rails', '4.0.1' # standard library gem 'sass-rails', '~> 4.0.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.0.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 1.2' group :doc do gem 'sdoc', require: false end # custom gem 'activeadmin', github: 'gregbell/active_admin' gem 'devise' gem 'simple_form' 

specifications / spec_helper:

 # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'capybara/rspec' # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) RSpec.configure do |config| config.include Capybara::DSL config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) DatabaseCleaner.start DatabaseCleaner.clean end config.before(:each) do DatabaseCleaner.clean end config.after(:each) do DatabaseCleaner.clean end config.after(:suite) do DatabaseCleaner.clean end # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" # config.include RSpec::Rails::RequestExampleGroup, type: :feature # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true # If true, the base class of anonymous controllers will be inferred # automatically. This will be the default behavior in future versions of # rspec-rails. config.infer_base_class_for_anonymous_controllers = false # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = "random" end 
+6
source share
4 answers

Do you want to change this

 config.after(:suite) do DatabaseCleaner.clean end 

For this:

 config.after(:suite) do DatabaseCleaner.clean_with(:truncation) end 

Otherwise, it will simply roll back the transaction, which will leave any data that existed before the start of the transaction.

+9
source

This works for me:

 DatabaseCleaner.strategy = :truncation ... before(:each) do DatabaseCleaner.clean end 
0
source

Please show the specification.

You must be sure that your setup / shutdown is performed in the operators before / after / etc.

If you have the setting and assignment of variables outside the above and just “in the test”, then the test will bomb how you are experiencing. If you configure, this problem can be avoided.

You do not need to try to squeeze the insides the way you do. As in many other cases on Ror land, if you do this, most likely you can "go off the rails" using your code. The rails are designed to be the foundation that makes all the boredom for you, you just need to stay “on the rails”.

0
source

to add a file:

 # RSpec # spec/support/database_cleaner.rb RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) end config.around(:each) do |example| DatabaseCleaner.cleaning do example.run end end end 

and uncomment

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

in spec/rails_heper.rb

0
source

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


All Articles