DatabaseCleaner 在 :transaction 上失败
DatabaseCleaner fails on :transaction
我有一个 Rails API,我正在尝试用 Rspec 进行测试。我在 Gemfile 和 运行 sudo bundle install
中为 mongoid
添加了 DatabaseCleaner,但出现以下错误。
我是否缺少其中任何一个配置?
The DatabaseCleaner::Mongoid adapter has been extracted to its own gem: database_cleaner-mongoid, and will be removed from database_cleaner in 2.0. To silence this message, please replace `gem "database_cleaner"` with `gem "database_cleaner-mongoid"` in your Gemfile.
F
Failures:
1) Users POST /api/v1/users creates a new user with valid details
Failure/Error: DatabaseCleaner.strategy = :transaction
DatabaseCleaner::UnknownStrategySpecified:
The 'transaction' strategy does not exist for the mongoid ORM! Available strategies: truncation
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:150:in `rescue in require_orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:146:in `require_orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:142:in `rescue in orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:133:in `orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:112:in `create_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:34:in `strategy='
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:19:in `block in strategy='
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:19:in `each'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:19:in `strategy='
# ./spec/rails_helper.rb:42:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
# uninitialized constant DatabaseCleaner::Mongoid::Transaction
# Did you mean? DatabaseCleaner::Mongoid::Truncation
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:135:in `const_get'
Finished in 0.18763 seconds (files took 2.8 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/users_request_spec.rb:18 # Users POST /api/v1/users creates a new user with valid details
Gemfile
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'database_cleaner-mongoid'
end
rails_helper.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
end
config.after(:each) do
DatabaseCleaner.clean
end
config.before(:all) do
DatabaseCleaner.start
end
config.after(:all) do
DatabaseCleaner.clean
end
end
该错误消息非常简单明了。你不能在 mongoid 上使用事务策略。该配置用于 ActiveRecord。
在切换策略之间的整个想法是事务比截断快得多。但是当 运行 测试异步时,您不能依赖事务来正确清除数据库。
当然,这不是 Mongoid 上的一个选项,数据库清理器甚至不再与 ActiveRecord 相关。
RSpec.configure do |config|
config.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.before(:all) do
DatabaseCleaner.start
end
config.after(:all) do
DatabaseCleaner.clean
end
end
我有一个 Rails API,我正在尝试用 Rspec 进行测试。我在 Gemfile 和 运行 sudo bundle install
中为 mongoid
添加了 DatabaseCleaner,但出现以下错误。
我是否缺少其中任何一个配置?
The DatabaseCleaner::Mongoid adapter has been extracted to its own gem: database_cleaner-mongoid, and will be removed from database_cleaner in 2.0. To silence this message, please replace `gem "database_cleaner"` with `gem "database_cleaner-mongoid"` in your Gemfile.
F
Failures:
1) Users POST /api/v1/users creates a new user with valid details
Failure/Error: DatabaseCleaner.strategy = :transaction
DatabaseCleaner::UnknownStrategySpecified:
The 'transaction' strategy does not exist for the mongoid ORM! Available strategies: truncation
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:150:in `rescue in require_orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:146:in `require_orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:142:in `rescue in orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:133:in `orm_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:112:in `create_strategy'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:34:in `strategy='
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:19:in `block in strategy='
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:19:in `each'
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/configuration.rb:19:in `strategy='
# ./spec/rails_helper.rb:42:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
# uninitialized constant DatabaseCleaner::Mongoid::Transaction
# Did you mean? DatabaseCleaner::Mongoid::Truncation
# /var/lib/gems/2.5.0/gems/database_cleaner-1.8.4/lib/database_cleaner/base.rb:135:in `const_get'
Finished in 0.18763 seconds (files took 2.8 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/users_request_spec.rb:18 # Users POST /api/v1/users creates a new user with valid details
Gemfile
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'database_cleaner-mongoid'
end
rails_helper.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
end
config.after(:each) do
DatabaseCleaner.clean
end
config.before(:all) do
DatabaseCleaner.start
end
config.after(:all) do
DatabaseCleaner.clean
end
end
该错误消息非常简单明了。你不能在 mongoid 上使用事务策略。该配置用于 ActiveRecord。
在切换策略之间的整个想法是事务比截断快得多。但是当 运行 测试异步时,您不能依赖事务来正确清除数据库。
当然,这不是 Mongoid 上的一个选项,数据库清理器甚至不再与 ActiveRecord 相关。
RSpec.configure do |config|
config.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.before(:all) do
DatabaseCleaner.start
end
config.after(:all) do
DatabaseCleaner.clean
end
end