RSpec + Javascript = BusyException:无法回滚事务

时间:2012-02-11 02:12:35

标签: rspec ruby-on-rails-3.2

我对此测试有疑问:

require File.dirname(__FILE__) + '/../spec_helper'

describe "registration feature", :js => true do
  it "sign up user" do
    @user = Factory.create(:user)
    visit login_path
    puts page.html
  end
end

它显示消息:SQLite3 :: BusyException:无法回滚事务 - 正在进行的SQL语句:回滚事务

我试过很多想法:

  • 当我删除“:js => true”时,它可以正常工作。
  • 当我尝试使用selenium或webkit时,它不起作用。
  • 我尝试删除数据库或重新启动我的电脑,但它无效。
  • 我尝试用mysql运行。

这是我的spec_helper:

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'
  require "email_spec"
  require 'simplecov'  
  require "authlogic/test_case"
  SimpleCov.start 'rails'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :mocha
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false

    config.include(EmailSpec::Helpers)
    config.include(EmailSpec::Matchers)
  end

  include Authlogic::TestCase

  require 'database_cleaner'
  DatabaseCleaner.strategy = :truncation

  Capybara.javascript_driver = :webkit
  Capybara.default_selector = :css
end

Spork.each_run do
  DatabaseCleaner.clean
  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/lib/**/*.rb"].each { |f| load f }
  Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
  I18n.backend.load_translations
end

这是我的test.rb:

Onopia::Application.configure do
  config.cache_classes = true
  config.serve_static_assets = true
  config.static_cache_control = "public, max-age=3600"
  config.whiny_nils = true
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_dispatch.show_exceptions = false
  config.action_controller.allow_forgery_protection    = false
  config.action_mailer.delivery_method = :test
  config.active_support.deprecation = :stderr
  config.active_record.mass_assignment_sanitizer = :strict
end

我真的不知道该怎么办。你能帮助我吗?非常感谢!

2 个答案:

答案 0 :(得分:3)

我找到了解决方案。我补充一点:

self.use_transactional_fixtures = false

描述之后。

答案 1 :(得分:0)

这是一个更好的解决方案:

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'
  require "email_spec"
  require 'simplecov'  
  require "authlogic/test_case"
  require 'database_cleaner'

  SimpleCov.start 'rails'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :mocha
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false

    config.before(:suite, :type => :request) do
      config.use_transactional_fixtures = false
      DatabaseCleaner.strategy = :truncation
    end

    config.before(:each, :type => :request) do
      DatabaseCleaner.start
    end

    config.after(:each, :type => :request) do
      DatabaseCleaner.clean
    end

    config.include(EmailSpec::Helpers)
    config.include(EmailSpec::Matchers)
  end

  include Authlogic::TestCase

  Capybara.javascript_driver = :webkit
  Capybara.default_selector = :css
end

Spork.each_run do
  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/lib/**/*.rb"].each { |f| load f }
  Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
  I18n.backend.load_translations
end