Rspec极其缓慢

我的rspec测试似乎运行速度非常慢,即使有防护和spork。

Finished in 5.36 seconds 13 examples, 2 failures 

我知道我可以做几件事来优化我的测试并减少与数据库的交互,但我强烈怀疑spec_helper设置不正确。 我在轨道3.2.11与mongoid。 每次运行后数据库清理程序都会清理。

 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 

您可以通过使用-p / --profile标志运行rspec来描述您的规范:

 rspec spec -p [-drb, and whatever else] 

这将列出10个最慢的例子及其执行时间。 您可以通过为-p标志提供可选计数来更改默认值10。 有关使用rspec --help更多信息