0

我正在使用此解决方案来避免在 javascript 测试期间出现数据库问题。

第一次运行套件,测试运行良好,全部通过。

如果我再次运行整个套件,它们仍然会通过。

但是,如果我运行一个单独的规范文件,然后尝试运行该套件(或其他单独的测试),我会收到此错误:

An error occurred in an after hook
  ActiveRecord::StatementInvalid: ArgumentError: prepare called on a closed database: rollback transaction
  occurred at /home/steveq/.rvm/gems/ruby-1.9.3-p194@rails32/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize'

1) Signing up with valid information
     Failure/Error: visit "/sign_up"
     ActiveRecord::StatementInvalid:
     ArgumentError: prepare called on a closed database: PRAGMA table_info("users")
 # ./app/controllers/registrations_controller.rb:3:in `new'
 # ./app/controllers/registrations_controller.rb:3:in `new'
 # ./spec/features/sign_up_feature_spec.rb:5:in `block (2 levels) in <top (required)>'

如果我重新加载警卫,测试将再次通过。

有没有人对这里发生的事情或任何可能的解决方案有任何见解?我已经尝试了所有我能想到的变体,这是我的 spec_helper 文件来显示我尝试过的东西(变体被注释掉,当前代码 - 水豚页面上的建议 - 是我现在正在使用的) .

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)


require 'rspec/rails'
  require 'rspec/autorun'

  # 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|
    #Make it so Selenium (out of thread) tests can work with transactional fixtures
    #REF http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#comment-220
    # ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
    #   def current_connection_id
    #     # Thread.current.object_id
    #     Thread.main.object_id
    #   end
    # end
    # FactoryGirl short syntax
    config.include FactoryGirl::Syntax::Methods

    config.use_transactional_fixtures = true

    # set up for use with :js => true. 
    # See http://stackoverflow.com/questions/8178120/capybara-with-js-true-causes-test-to-fail for more info 
    # config.before :suite do
    #   if Capybara.current_driver == :rack_test
    #     DatabaseCleaner.strategy = :transaction
    #   else
    #     DatabaseCleaner.strategy = :truncation
    #   end
    #   DatabaseCleaner.start
    # end

    # config.after do
    #   DatabaseCleaner.clean
    # end

    # standard RSPEC config
    # config.before(:suite)       :truncation
    #   else
    #     :transaction
    #   end do
    #   DatabaseCleaner.strategy = if example.metadata[:js]
    #     :truncation
    #   else
    #     :transaction
    #   end
    #   DatabaseCleaner.clean_with(:truncation)
    # end

    # config.before(:each) do
    #   DatabaseCleaner.start
    # end

    # config.after(:each) do
    #   DatabaseCleaner.clean
    # end

    # config.before(:each) do
    #   DatabaseCleaner.strategy = if example.metadata[:js]
    #     :truncation
    #   else
    #     :transaction
    #   end
    #   DatabaseCleaner.start
    # end

    # config.after(:each) do
    #   DatabaseCleaner.clean
    # end

    # 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

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true

    config.include MailerMacros
    config.include LoginMacros
    config.before(:each) { reset_email }

    config.include Devise::TestHelpers, :type => :controller
    config.extend LoginMacros, :type => :controller
  end
end

Spork.each_run do
  # allows capybara JS tests to run in separate thread 
  class ActiveRecord::Base
    mattr_accessor :shared_connection
    @@shared_connection = nil

    def self.connection
      @@shared_connection || retrieve_connection
    end
  end

  # Forces all threads to share the same connection. This works on
  # Capybara because it starts the web server in a thread.
  ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

  # This code will be run each time you run your specs.
  load "#{Rails.root}/config/routes.rb" 
  FactoryGirl.reload
  # reload all the models
  Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
    load model
  end


end
4

1 回答 1

1

所以这就是我想出的——希望它能帮助遇到同样麻烦的其他人。

首先,我使用的方法:

Spork.each_run do
  # allows capybara JS tests to run in separate thread 
  class ActiveRecord::Base
    mattr_accessor :shared_connection
    @@shared_connection = nil

    def self.connection
      @@shared_connection || retrieve_connection
    end
  end

  # Forces all threads to share the same connection. This works on
  # Capybara because it starts the web server in a thread.
  ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

  # This code will be run each time you run your specs.
  load "#{Rails.root}/config/routes.rb" 
  FactoryGirl.reload
  # reload all the models
  Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
    load model
  end


end

工作得很好,但似乎不适用于 sqlite。

最快的解决方法是简单地将 sqlite 换成我的 mysql 测试数据库。这解决了一切。

另一种解决方案,我现在真正在挖掘的一个解决方案是完全放弃 spork 以支持Zeus

你可以通过 github 链接查看它,但我会告诉你为什么我喜欢它。

它没有必要的配置——spec_helper 中没有 spork 块,guardfile 中没有 spork 块。

它还可以将服务器和控制台的初始化速度加快到不到一秒钟——这不是什么大不了的事,但非常非常令人愉快。

我的测试套件(到目前为止有 191 个示例)从运行大约 35 秒到 17.5 秒 - 一半的时间。

我敦促你检查一下。

于 2013-03-28T20:03:30.717 回答