2012-08-31 35 views
7

Khi cố gắng để làm theo ví dụ trên database_cleaner của GitHub page, tôi gặp phải lỗi sau từ RSpec:SQLite3 :: SQLException khi sử dụng database_cleaner với Rails/Spork/RSpec

ActiveRecord::StatementInvalid: 
    SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction 

Cấu hình sử dụng trong spec_helper.rb là:

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 
+0

Tôi gặp lỗi tương tự khi tạo bản ghi, có thể bạn có thể trợ giúp? http://stackoverflow.com/questions/14367396/sql-error-cannot-start-a-transaction-within-a-transaction-while-testing-with-cuc – pahnin

Trả lời

6

Tôi đã tìm thấy giải pháp thay đổi toàn bộ chiến lược thành :truncation. Đã cập nhật 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 
+0

xuất sắc - cảm ơn sự giúp đỡ! – bonhoffer

9

Câu trả lời được chấp nhận làm cho tất cả các kiểm tra chậm hơn (khi không cần) bằng cách cắt xén sau mỗi lần.

Chỉ cần thêm

config.use_transactional_fixtures = false 

khi sử dụng database_cleaner.

Nếu bạn có cả hai số config.use_transactional_fixtures = trueDatabaseCleaner.strategy = :transaction, bạn sẽ bắt đầu giao dịch bên trong một giao dịch khác và không được phép.

+0

Đã lâu rồi kể từ khi tôi gặp sự cố này. Tôi nghĩ rằng tôi có thể đã thử điều đó. Hãy quên đi dự án nào, vì vậy tôi không thể kiểm tra lại = \ – Dan

+0

@ Tôi có thể nghĩ rằng có thể đã xảy ra, nhưng tôi đã trả lời để giữ một kỷ lục cho độc giả trong tương lai. –

Các vấn đề liên quan