即使使用 guard 和 spork,我的 rspec 测试似乎也运行得非常缓慢。
Finished in 5.36 seconds
13 examples, 2 failures
我知道我可以做几件事来优化我的测试并减少与数据库的交互,但我强烈怀疑 spec_helper 的设置不正确。我在使用 mongoid 的 Rails 3.2.11 上。每次运行后都会清理数据库清理器。
spec_helper.rb
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'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
DatabaseCleaner[:mongoid].strategy = :truncation
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.filter_run focus: true
config.filter_run_excluding :remove => true
config.run_all_when_everything_filtered = true
config.include Mongoid::Matchers
config.include Capybara::DSL
ActiveSupport::Dependencies.clear
end
end
Spork.each_run do
Fabrication.clear_definitions
RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.clean
end
end
end
更新:问题出在我的一项测试上。花了3秒钟。请检查@Sam Peacey 对我用来获得以下结果的命令的回答
Dynamic Model should destroy collection when related source is destroyed
2.46 seconds ./spec/models/dynamic_model_spec.rb:10
Dynamic Model Validations should validate uniqueness
0.66357 seconds ./spec/models/dynamic_model_spec.rb:69