Dan*_*Dan 7 sqlite rspec ruby-on-rails spork
当试图在database_cleaner的GitHub页面上关注示例时,我从RSpec遇到以下错误:
ActiveRecord::StatementInvalid:
SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction
Run Code Online (Sandbox Code Playgroud)
spec_helper.rb中使用的配置是:
require 'spork'
require 'database_cleaner'
Spork.prefork do
# .. snip
RSpec.configure do |config|
# .. snip
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
end
Run Code Online (Sandbox Code Playgroud)
接受的答案通过在每个测试之后截断来使所有测试变慢(当不需要时).
只需添加
config.use_transactional_fixtures = false
Run Code Online (Sandbox Code Playgroud)
使用database_cleaner时.
如果你有两个config.use_transactional_fixtures = true
并且DatabaseCleaner.strategy = :transaction
你将在另一个交易中开始交易,那是不允许的.
我发现解决方案是将整个策略改为:truncation
.更新了spec_helper:
require 'spork'
require 'database_cleaner'
Spork.prefork do
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3235 次 |
最近记录: |