【发布时间】:2012-03-19 22:17:32
【问题描述】:
我尝试关注José Valim's advice on faster test suites,但无济于事。
我使用 Spork 并将 AR 猴子补丁放在 Spork.each_run 块中(请参阅下面的规范助手)。
但是,我的请求规范失败了,因为在运行之间没有清理数据库 - 具体来说,在执行 Model.should have(1).record 之类的断言时,我收到了诸如 expected 1 record, got X 之类的错误。
更新
问题在于使用 Javascript 的请求规范。请参阅以下规范 - 当我使用 js: true 时它会失败,但如果我删除它则不会(我使用 RSpec 的 config.treat_symbols_as_metadata_keys_with_true_values = true,fwiw。)
# 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
我可以判断它与数据库未清理有关,因为它第一次失败(expected 1 record, got 0),第二次通过(因为数据库中有 1 条记录)然后失败在任何后续运行中再次运行(expected 1 record, got 2 等)
我试图避免使用像 DatabaseCleaner 这样的 gem,尽可能减少依赖关系,并避免测试套件的速度下降。
非常感谢任何帮助/信息/指针!
相关信息:
- Rails 3.2.2
- RSpec 2.9.0
- 水豚1.1.2
- Capybara-webkit 0.11.0
- FactoryGirl 2.6.4
- Spork 0.9.0
- Guard 1.0.1
Guard-spork 0.5.2
在 Macbook Air、OS X 10.7.3(如果相关)上
还有我的规范助手:
# 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
【问题讨论】:
-
规范/支持有什么相关的吗?你能展示一个失败的规范吗?没有spork也能用吗?
-
我没有规范/支持。当我在没有 Spork 的情况下运行规范时问题是一样的 - 数据库没有被清理。我应该提到它特定于使用 javascript 的规范 - 我将使用失败的规范更新问题。
-
使用失败的规范更新问题。谢谢cuvius!
标签: ruby-on-rails testing activerecord rspec spork