1

即使有后卫& spork,我的rspec测试似乎也运行得非常缓慢。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 

UPDATE:问题是与我的测试之一。这需要3秒钟。请检查@Sam皮西对命令的回答经常拿下面的结果

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 
+0

5秒没那么糟糕。 Ruby不是C你知道... – tjameson 2013-03-18 07:37:20

+1

13个测试?我已经看到人们在5-6秒内运行200次以上的测试。差异似乎太大。 – Rahul 2013-03-18 07:41:14

+0

我已经有了Ruby虚拟机需要3-4秒才能启动,这是在最近的硬件。 – tjameson 2013-03-18 07:43:49

回答

9

您可以通过与-p/--profile标志运行rspec的分析您的规格:

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

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