RSpec + database cleaner, not cleaning correctly - ruby-on-rails

i really can't understand why DatabaseCleaner is not cleaning my test database. This is what i get prompting
1.9.2p290 :007 > DatabaseCleaner.clean
--
=> [#<DatabaseCleaner::Base:0x007fa7e4dd8b58 #autodetected=true, #orm=:active_record, #strategy=#<DatabaseCleaner::ActiveRecord::Transaction:0x007fa7e4dc14f8 #db=:default>>]
This seems not to set correctly the database (supposed to be :test), so i got a solution like
DatabaseCleaner[:active_record, :connection => :test].clean
# => nil
The gem seems to be set up correctly:
1.9.2p290 :007 > DatabaseCleaner[:active_record, :connection => :test]
#<DatabaseCleaner::Base:0x007fe8fcfd4868 #orm=:active_record, #strategy=#<DatabaseCleaner::ActiveRecord::Transaction:0x007fe8fcfd2748 #db=:test, #connection_hash={"adapter"=>"sqlite3", "database"=>"db/test.sqlite3", "pool"=>5, "timeout"=>5000}>, #db=:test>
This seems to setup correctly the test database, however it is still not going to clean properly the database. Any suggestions?
Thank you very much.

Even with database cleaner correctly configured it is easy to leave data lying around.
config.before(:suite) do
DatabaseCleaner.clean_with :truncation # clean DB of any leftover data
DatabaseCleaner.strategy = :transaction # rollback transactions between each test
Rails.application.load_seed # (optional) seed DB
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
The configuration above starts & runs the cleaner either side of each test.
If you then use before :all in your specs you can end up with data lying around:
describe User do
# Before all is outside the before :each
before :all do
#user = User.create(:email => 'hello#example.com')
end
...tests here
end

Here's my spec_helper.rb (slightly modified) - maybe that will help you?
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end

Avdi Grimm's had a nice post recently on configuring database cleaner:
http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

$ rails c test
> require 'database_cleaner'
> DatabaseCleaner.strategy = :truncation
> DatabaseCleaner.clean
https://github.com/DatabaseCleaner/database_cleaner

Related

Resetting table IDs and re-creating the test DB with Rspec on Rails

I have various tests that create models using FactoryGirl in Rails
it "does something" do
user = create(:user)
...
expect(user.name).to eq("foo")
end
I noticed that each time I run the spec suite, it does not reset the table id's on the test schema (using Postgres). So after several executions the user might have an id of something like 2894.
I use Database Cleaner to clean my DB between subsequent runs - see below.
Is there a way to reset table ID's directly between rspec runs? A company I previously worked at did it with their app, but I'm not aware how.
If #1 is not an option, is it possible to drop and recreate the test DB between runs?
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:deletion)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :deletion
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
I am also using DatabaseCleaner gem and don't experience the same issue you do (though I'm using mysql instead).
Not sure if this helps in your case but would be worth trying my configuration:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
Rails.application.load_seed
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
My rails_helper.rb
require 'support/database_cleaner'
RSpec.configure do |config|
config.use_transactional_fixtures = false

How to populate and clean a database in ruby on rails with a rake task and database cleaner gem?

I have two rake tasks in one file. seed and populate.
seed is creating a necessary data, and populate is populating sample data for my tests.
In my tests I am manipulating a database(adding the new entry, removing it and etc.). I am using Database cleaner gem to reseting the database However when I reset the database I need the populate data for the next test. I have the following setting but it is not working the database is still empty and the populate data is not there .
this is the spec/support/database_cleaner.rb
require 'rake'
load File.expand_path("../../../lib/tasks/my_tasks.rake", __FILE__)
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
Rake::Task.define_task(:environment)
Rake::Task['db:seed'].invoke
Rake::Task['db:populate'].invoke
end
config.after(:each) do
DatabaseCleaner.clean
end
end
spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'rake'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'factory_girl_rails'
require 'rspec/rails'
require 'capybara/poltergeist'
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
Capybara.configure do |config|
config.default_wait_time = 20
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryGirl::Syntax::Methods
# 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.infer_spec_type_from_file_location!
end
I found a way around it but this is not perfect and I am still looking for the solution of this so please post an answer if you find it. I am not going to select this as the correct answer.
You need to place a content spec/support/database_cleaner.rb file into the spec/spec_helper.rb file.
RSpec.configure do |config|
.
.
.
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
Rake::Task.define_task(:environment)
Rake::Task['db:seed'].invoke
Rake::Task['db:populate'].invoke
end
config.after(:each) do
# DatabaseCleaner.clean
end
config.infer_spec_type_from_file_location!
end
Important: You need to comment or delete the: DatabaseCleaner.clean

Clear database after testing a rake task using RSpec

I'm trying to test my rake task using rspec and it runs fine, but the problem is that the records aren't being deleted afterwards.
I've put config.use_transactional_fixtures = true in the config file to no affect. In other tests it's was working fine.
Here's my code:
require 'rspec'
require 'spec_helper'
require 'rake'
describe 'my_app rake tasks' do
describe 'rake_task_1' do
before { MyApp::Application.load_tasks }
it 'should test it!' do
10.times { create(:record) }
Rake::Task["rake_task_1"].invoke
Record.count.should == 10
end
end
end
In my rake task I'm executing a query using ActiveRecord::Base.connection.execute.
I'm sure it has something with SQL transitions in RSepc....
any ideas?
Have you tried using database_cleaner gem?
You can check the great article by Avdi Grimm about how to configure it.
config.use_transactional_fixtures = false
In file spec/support/database_cleaner.rb:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end

Capybara can't find database records during feature specs

I have a JS feature spec I'm trying to run with Capybara Webkit. It doesn't seem to be able to find my database records however.
The spec in question looks like this
it "should allow pledging to a Hardback level", js: true do
book = FactoryGirl.create :book
visit book_path(book)
click_link "pledge-btn"
end
Unfortunately, the request to book_path(book) 404s because the book cannot be found.
If I take the :js flag off, the test passes.
I have DatabaseCleaner set up to use :truncation for JS specs as is the recommended method.
# spec/support/database_cleaner.rb
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
DatabaseMetadata.create!(:sanitized_at => DateTime.now)
end
config.after(:each) do
DatabaseCleaner.clean
end
end
I can puts the book in the test and it will be found.
it "should allow pledging to a Hardback level", js: true do
book = FactoryGirl.create :book
visit book_path(book)
p Book.first
# => .. the book instance
click_link "pledge-btn"
end
I've also tried this shared connection method which doesn't seem to fix the problem either.
What else could be wrong?
You may have config.use_transactional_fixtures = true set in your spec_helper.rb. This would override what you have above.
You want to either remove this line from your spec_helper.rb or change it there to be false.
I ran into this same issue and had a very similar config. After looking through the DatabaseCleaner README, I found this small note:
It's also recommended to use append_after to ensure DatabaseCleaner.clean runs after the after-test cleanup capybara/rspec installs.
Source
That means changing your
config.after(:each) do
DatabaseCleaner.clean
end
to
config.append_after(:each) do
DatabaseCleaner.clean
end
Note the append_after instead of after. This fixed my problem.
Working on legacy project I have had such issue, it was caused by switching DatabaseCleaner strategy to :truncation like the following:
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean
Test::Seeder.seed!
DatabaseCleaner.strategy = :transaction
end
so, removing DatabaseCleaner.strategy = :transaction helped in my case
If you create records in a before(:all) block then they will be available.
before :all do
#book = FactoryGirl.create :book
end
it "should allow pledging to a Hardback level", js: true do
visit book_path(#book)
click_link "pledge-btn"
end
Capybara runs the rails server in a separate process from the tests, so they each get their own connection to the database. Therefore, the server does not access to the records created in the transaction for the test.
Because they are not inside a transaction, make sure that you clean them up with DatabaseCleaner in your spec_helper.rb:
config.after(:all, :type => :feature) do
DatabaseCleaner.clean
end
For anyone that lands here in 2019 and beyond, I was caught out by the following code which I copied verbatim from the DatabaseCleaner readme:
config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
unless driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
end
end
This is all well and good, but I am using Rails system specs, not RSpec features and therefore this code block was never being run.
Change config.before(:each, type: :feature) to config.before(:each, type: :system) if you're using system specs!
I think your main issue is that your rails_helper.rb has the following line commented out:
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
This means your database_cleaner.rb is never getting loaded.

Why is my test database is not being truncated before tests are being ran?

I am running tests in cucumber and would like the database to be empty for the tests I am running. I've configured the settings to do this but data exists when I run the tests. What am I doing wrong? Also I am using mongoid and am using MongoDB.
features/support/env.rb:
require 'cucumber/rails'
Capybara.default_selector = :css
Dir[(Rails.root + "spec/factories/**/*.rb").to_s].each {|factory| require factory}
ActionController::Base.allow_rescue = false
begin
DatabaseCleaner.strategy = :truncation
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Cucumber::Rails::Database.javascript_strategy = :truncation
if defined?(ActiveRecord::Base)
begin
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
rescue LoadError => ignore_if_database_cleaner_not_present
end
end
You aren't actually cleaning the database at any point. You have Database Cleaner, but aren't calling the clean method.
Try this in env.rb
Before do
DatabaseCleaner.start
end
After do
DatabaseCleaner.clean
end

Resources