Rails + Rspec:数据库清理程序gem不起作用

时间:2017-11-12 04:17:48

标签: ruby-on-rails rspec capybara factory-bot database-cleaner

我需要一些关于如何让数据库清理器为我的rails应用程序工作的指导。每个测试的记录ID不断增加,并且没有被删除。

我已经设置了一个测试来检查数据库清理程序是否正常工作并且失败了。

规格/模型/关切/ 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

故障:

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

       expected: 1
            got: 2

       (compared using ==)

设定:

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

这是我在rails测试控制台中看到的并输入

DatabaseCleaner.clean

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

我已从自述文件中复制并粘贴了数据库清理程序推荐的配置,但它不起作用。 (我使用rspec和capybara webkit)这是我的完整 rails_helper.rb 文件:

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

我也引用了本教程并调整了rspec配置,但这也无济于事。

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

1 个答案:

答案 0 :(得分:0)

使用数据库清理程序的自动增量ID的行为取决于所使用的特定数据库以及您正在使用的清除方法(截断,删除,事务)。话虽如此,它应该与您的测试无关,因为它们根本不应该依赖于特定的ID。