Rah*_*hul 3 ruby-on-rails spork rspec-rails ruby-on-rails-3 database-cleaner
我的rspec测试似乎运行速度非常慢,即使有防护和spork.
Finished in 5.36 seconds
13 examples, 2 failures
Run Code Online (Sandbox Code Playgroud)
我知道我可以做几件事来优化我的测试并减少与数据库的交互,但我强烈怀疑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
Run Code Online (Sandbox Code Playgroud)
更新:问题出在我的一个测试中.这需要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
Run Code Online (Sandbox Code Playgroud)
Sam*_*cey 12
您可以通过使用-p/ --profileflag 运行rspec来描述您的规范:
rspec spec -p [-drb, and whatever else]
Run Code Online (Sandbox Code Playgroud)
这将列出10个最慢的示例及其执行时间.您可以通过为-p标志提供可选计数来更改默认值10.更多信息使用rspec --help
| 归档时间: |
|
| 查看次数: |
4717 次 |
| 最近记录: |