【问题标题】:SQLite exception with RSpec, Spork and Database CleanerRSpec、Spork 和 Database Cleaner 的 SQLite 异常
【发布时间】:2012-10-14 00:15:12
【问题描述】:

运行大约 5 次后效果很好,但随后所有测试都失败了:

ArgumentError: prepare called on a closed database:           SELECT name
             FROM sqlite_master
             WHERE type = 'table' AND NOT name = 'sqlite_sequence'

没有 Spork 就不会发生,如果我重新启动 Spork 就可以正常工作。运行一定次数后不会发生,但每次都会改变。

有什么可能导致这种情况的想法吗?

编辑:

仅在更改控制器代码时发生。

spec_helper.rb:

require 'spork'
require 'simplecov'
SimpleCov.start

Spork.prefork do
  ENV['RAILS_ENV'] ||= 'test'

  require File.expand_path('../../config/environment', __FILE__)
  require 'rspec/rails'
end

Spork.each_run do # This code will be run each time you run your specs.
  require 'capybara/rspec'
  require 'capybara/rails'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join('spec/support/**/*.rb')].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :rspec

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false

    config.include RequestHelpers, :type => :request

    config.before :suite do
      DatabaseCleaner.strategy = :truncation
      DatabaseCleaner.clean_with :truncation
    end

    config.before :each do
      DatabaseCleaner.start
    end

    config.after :each do
      DatabaseCleaner.clean
    end

    config.include(MailerHelpers)
    config.before(:each) { reset_email }
  end
end

【问题讨论】:

    标签: ruby-on-rails rspec sqlite spork


    【解决方案1】:

    尝试删除此行:

    config.use_transactional_fixtures = true
    

    似乎对我有用。我被错误消息引导,因为它与交易相关,但不完全确定交易是什么。似乎与 spork 的线程和 sqlite3 有关

    【讨论】:

      【解决方案2】:

      我经常遇到这个错误,并且能够通过更新到最新版本的 sqlite3 来修复它。

      bundle update sqlite3
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-20
        • 2011-12-31
        • 1970-01-01
        相关资源
        最近更新 更多