Ruby on rails Rspec非常慢
即使使用guard&spork,我的rspec测试似乎运行得非常慢Ruby on rails Rspec非常慢,ruby-on-rails,ruby-on-rails-3,rspec-rails,spork,database-cleaner,Ruby On Rails,Ruby On Rails 3,Rspec Rails,Spork,Database Cleaner,即使使用guard&spork,我的rspec测试似乎运行得非常慢 Finished in 5.36 seconds 13 examples, 2 failures 我知道我可以做一些事情来优化我的测试并减少与数据库的交互,但我强烈怀疑spec_助手的设置不正确。我在mongoid的rails 3.2.11上。每次运行后,数据库清理器都会进行清理 spec_helper.rb require 'rubygems' require 'spork' Spork.prefork do ENV[
Finished in 5.36 seconds
13 examples, 2 failures
我知道我可以做一些事情来优化我的测试并减少与数据库的交互,但我强烈怀疑spec_助手的设置不正确。我在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
您可以通过运行带有
-p
/--profile
标志的rspec来配置规格:
rspec spec -p [-drb, and whatever else]
这将列出10个执行时间最慢的示例。您可以通过为-p标志提供可选计数来更改默认值10。使用
rspec--help了解更多信息在我的例子中,问题是rspec在development
ENV中运行,而不是在test
中运行。我通过在spec\u helper.rb
中添加ENV['RAILS\u ENV'].\124;='test'
作为第一行来修复它,否则它就不起作用了
另外,我必须推荐非常有用的gem来分析代码-5秒也没那么糟糕。Ruby不是C吗?你知道…13次测试?我见过人们在5-6秒内运行200多个测试。差别似乎太大了。我让Ruby虚拟机只需3-4秒就可以启动,这是在最近的硬件上实现的。这是正确的。但是Rspec不从它的日志中排除这个时间吗?所以5秒应该只处理测试,对吗?我同意这对于13个测试来说是很慢的,您是否尝试过删除数据库清理器调用,只是为了看看这是否是罪魁祸首(您的一些测试很可能会失败,但您可能会发现问题出在哪里)?