-1

I need some guidance on how to get database cleaner to work for my rails app. ID of records keep on increasing for each test and is not being wiped out.

I've set up a test to check if database cleaner works and it fails.

spec/models/concerns/database_cleaner_test.rb

require 'rails_helper'

describe "db_cleaner" do

    let!(:article) { FactoryGirl.create(:article) }

    it "first test" do
      expect(Article.all.count).to eq(1)
      expect(Article.last.id).to eq(1)
    end

    it "should clean db" do
      expect(Article.all.count).to eq(1)
      expect(Article.last.id).to eq(1)
    end

end

Failures:

  1) db_cleaner should clean db
     Failure/Error: expect(Article.last.id).to eq(1)

       expected: 1
            got: 2

       (compared using ==)

Setup:

gem 'rails', '~> 5.1.2'
gem 'pg', '~> 0.18'

group :development, :test do
  gem 'rspec-rails', '~> 3.6', '>= 3.6.1'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'capybara', '2.7.1'
  gem 'capybara-webkit'
  gem 'database_cleaner', '~> 1.6', '>= 1.6.2'
  gem 'capybara-screenshot'
  gem 'factory_girl_rails', '~> 4.8'
end

Here is what i see in my rails test console and type in

DatabaseCleaner.clean

 => [#<DatabaseCleaner::Base:0x00000005e09368 @orm=:active_record, @strategy=#<DatabaseCleaner::ActiveRecord::Transaction:0x00000005e08f58 @db=:default, @connection_class=ActiveRecord::Base>, @db=:default>] 

I have copied and pasted the database-cleaner recommended configuration from the readme and it does not work. (i'm using rspec with capybara webkit) Here is my full rails_helper.rb file:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
Capybara.javascript_driver = :webkit
Capybara.server = :puma
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/webkit/matchers'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Capybara.ignore_hidden_elements = false

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
RSpec.configure do |config|

  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
    # :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

    if !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

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end

  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
  config.include FactoryGirl::Syntax::Methods
  config.include(Capybara::Webkit::RspecMatchers, :type => :feature)
  config.include Warden::Test::Helpers
  Warden.test_mode!
  config.after :each do
    Warden.test_reset!
  end
end

Capybara::Webkit.configure do |config|
  config.block_unknown_urls
end

RSpec::Matchers.define :appear_before do |later_content|
  match do |earlier_content|
    page.body.index(earlier_content) < page.body.index(later_content)
  end
end

I have also referenced this tutorial and tweaked the rspec config, but this too does not help.

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'database_cleaner'
require 'spec_helper'
require 'rspec/rails'
Capybara.javascript_driver = :webkit
Capybara.server = :puma
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/webkit/matchers'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Capybara.ignore_hidden_elements = false
Capybara::Screenshot.autosave_on_failure = false

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
  config.include FactoryGirl::Syntax::Methods
  config.include(Capybara::Webkit::RspecMatchers, :type => :feature)
  config.include Warden::Test::Helpers
  Warden.test_mode!
  config.after :each do
    Warden.test_reset!
  end

  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::Webkit.configure do |config|
  config.block_unknown_urls
end
doyz
  • 887
  • 2
  • 18
  • 43

1 Answers1

0

The behavior of auto increment ids with database cleaner is dependent on the specific database used and the method of cleaning you are using (truncation, deletion, transaction). That being said, it should be irrelevant to your tests since they shouldn't be depending on specific ids at all.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78