私のレールアプリでデータベースクリーナーを動作させる方法に関するガイダンスが必要です。レコードのIDは各テストごとに増加し続け、抹消されません。Rails + Rspec:データベースクリーナーの宝石は機能していません
私は、データベースクリーナーが機能して失敗するかどうかを確認するテストを設定しました。
仕様/モデル/懸念/ 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
ここでは、私は私のレールに見えるものですテストコンソールa ndタイプ
DatabaseCleaner.clean
=> [#<DatabaseCleaner::Base:0x00000005e09368 @orm=:active_record, @strategy=#<DatabaseCleaner::ActiveRecord::Transaction:0x00000005e08f58 @db=:default, @connection_class=ActiveRecord::Base>, @db=:default>]
readmeからデータベースクリーナーの推奨設定をコピー&ペーストしても動作しません。
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の設定を微調整し、これはあまりにも助けにはならないしている:ここでは私の完全なrails_helper.rbファイルがある(私はカピバラのWebKitのと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
あなたは共有できる問題を再現したレポを持っていますか? –
実際には、 'rspec-rails'が動作しているようなトランザクション制御(' count'期待が壊れていないため)です。シーケンシャルIDだけがリセットされません...あなたのテストではなぜそれは問題ですか? –