简体   繁体   English

RSpec-共享的数据库连接 和交易装置不起作用

[英]RSpec - shared DB conn. & transactional fixtures not working

I tried to follow José Valim's advice on faster test suites , but to no avail. 我尝试遵循JoséValim关于更快的测试套件的建议 ,但无济于事。 I use Spork and put the AR monkey patch in the Spork.each_run block (see the spec helper below). 我使用Spork并将AR猴子补丁放入Spork.each_run块中(请参阅下面的规范帮助器)。

However, my request specs fail because the database is not cleaned between runs - specifically, I get errors such as expected 1 record, got X when doing assertions like Model.should have(1).record . 但是,我的请求规范失败,因为在两次运行之间没有清理数据库-特别是,我得到诸如expected 1 record, got X错误expected 1 record, got X在执行诸如Model.should have(1).record类的断言时expected 1 record, got X

Update The problem lies with request specs using Javascript. 更新问题在于使用Javascript的请求规范。 See the following spec - it fails when I use js: true , but not if I remove that (I use RSpec's config.treat_symbols_as_metadata_keys_with_true_values = true , fwiw.) 请参阅以下规范-当我使用js: true时,它会失败js: true ,但是如果我删除它,则不会失败(我使用RSpec的config.treat_symbols_as_metadata_keys_with_true_values = true ,是第一次)。

# encoding: UTF-8

require 'spec_helper'

feature "Create a shift", :js do
  scenario %q(
    In order to plan the workload
    As a coordinator
    I want to schedule shifts
    And I want to see when they're scheduled for
    ) do
      visit shifts_path
      click_link "new_shift_#{Date.current}"
      fill_in 'shift_note', with: 'Casper - luk'
      click_link_or_button 'submit'

      Shift.should have(1).record
      Shift.last.begins_at.should == Date.current

      page.should have_selector("ol[data-date=\"#{Date.current}\"] li#shift_#{Shift.last.id}")
    end
end

I can tell it's related to the DB not being cleaned, because it fails the first time ( expected 1 record, got 0 ), passes the second time (because there's 1 record in the DB) and then fails again on any subsequent runs ( expected 1 record, got 2 etc.) 我可以说这与未清除数据库有关,因为它第一次失败( expected 1 record, got 0 ), 第二次通过 (因为数据库中有1条记录),然后在随后的任何运行中再次失败( expected 1 record, got 2类推)

I'm trying to avoid using a gem like DatabaseCleaner, to keep dependencies as few as possible, and to avoid a speed decrease in the test suite. 我试图避免使用像DatabaseCleaner这样的gem,以尽可能减少依赖项,并避免测试套件的速度降低。

Any help/info/pointers are greatly appreciated! 任何帮助/信息/指针,不胜感激!

Relevant info: 相关信息:

  • Rails 3.2.2 Rails 3.2.2
  • RSpec 2.9.0 RSpec 2.9.0
  • Capybara 1.1.2 水豚1.1.2
  • Capybara-webkit 0.11.0 Capybara-webkit 0.11.0
  • FactoryGirl 2.6.4 FactoryGirl 2.6.4
  • Spork 0.9.0 猪肉0.9.0
  • Guard 1.0.1 守卫1.0.1
  • Guard-spork 0.5.2 护卫队0.5.2

  • on a Macbook Air, OS X 10.7.3 (if that's relevant) 在Macbook Air上,OS X 10.7.3(如果相关)

And my spec helper: 而我的规范助手:

# encoding: UTF-8

require 'rubygems'
require 'spork'

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

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

  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = true
    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.infer_base_class_for_anonymous_controllers = false

    config.include Factory::Syntax::Methods

    Capybara.javascript_driver = :webkit

    Rails.logger.level = 4 # speed - http://blog.plataformatec.com.br/tag/capybara/
  end
end

Spork.each_run do
  require 'factory_girl_rails'

  class ActiveRecord::Base
    mattr_accessor :shared_connection
    @@shared_connection = nil

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

  ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
end

After much investigating, the problem seems to be with specs using JS in general, and not really the AR monkey patch. 经过大量调查,问题似乎出在一般使用JS的规范上,而不是真正的AR猴子补丁。

I've re-phrased the problem in a new question here: RSpec+Capybara request specs w/ JS not working 我在这里的一个新问题中重新表述了该问题: RSpec + Capybara请求带有JS的规范不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM