Rspec,防护,spork和capybara / selenium - js测试的配置

时间:2013-03-22 22:36:48

标签: selenium rspec capybara guard spork

我使用this solution来避免在javascript测试期间数据库出现问题。

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

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

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

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

1 个答案:

答案 0 :(得分: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块,保护文件中没有spork块。

它还可以将服务器和控制台的初始化速度提高到第二级 - 不是很大,但非常非常愉快。

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

我劝你检查一下。