【发布时间】:2014-04-03 19:19:10
【问题描述】:
我正在通过 Vagrant box(Ubuntu 32 位、Rails 4.0.3、Ruby 2.1.0p0)开发 Rails 项目。
我刚刚尝试将 Spork 添加到我的项目中以加快我的测试速度(使用 RSpec、Capybara),但我发现测试速度明显变慢。如果我只运行“rspec .”,我的所有测试都会在 5.83 秒内通过。但是,当我通过“guard -c -p”运行保护时,我保存了一个规范文件,我得到了 26.08 秒的时间。
注意:我必须运行“guard -p”才能真正让守卫通过 Vagrant 对文件保存运行我的测试。
当守卫开始运行测试时,它会显示参数:
["--color", "--failure-exit-code", "2", "--format", "progress", "--format",
"Guard::RSpec::Formatter", "--require", "/home/vagrant/.rvm/gems/ruby-2.1.0/
gems/guard-rspec-4.2.7/lib/guard/rspec/formatter.rb", "spec"]...
我看到“--format”被列出了两次,而“--drb”根本没有出现。
这是我的 Guardfile:
guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' }, :test_unit => false do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
end
guard :rspec, :cmd => 'rspec --drb' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
end
guard 'livereload' do
watch(%r{app/views/.+\.(erb|haml|slim)$})
watch(%r{app/helpers/.+\.rb})
watch(%r{public/.+\.(css|js|html)})
watch(%r{config/locales/.+\.yml})
# Rails Assets Pipeline
watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
end
这是我的 spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
#Devise
config.include Devise::TestHelpers, type: :controller
#Capybara
config.include Capybara::DSL
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
我的 .rspec 中唯一的东西:
--color
我的Gemfile的相关部分:
group :development, :test do
gem 'rspec-rails', '~> 2.0'
gem 'factory_girl_rails'
gem 'guard-rspec'
gem 'guard-livereload'
gem 'spork-rails'
gem 'guard-spork'
end
group :test do
gem 'shoulda-matchers'
gem 'capybara'
end
我注意到,如果我有警卫运行,然后保存一个文件,有时我会得到一个错误:
Could not start Spork server for rspec after 30 seconds. I will continue
waiting for a further 60 seconds.
我不知道为什么在安装 Spork 后花了这么长时间,尤其是当 rspec 通过同一个 Vagrant 框时要快得多。有什么想法吗?
【问题讨论】:
-
如果我删除 -p 标志,测试速度会提高到大约 5 秒(目前只有 12 个测试,但它正在通过 Vagrant 运行,所以也许我会看到速度随着时间的推移而增加) .但是,我必须在运行 Guard 的控制台窗口中手动按“输入”,它将运行所有测试,而不是从文件更改中运行。
-
我在这里找到了一个讨论:github.com/guard/guard-rspec/issues/183 基本上问题似乎在于 VirtualBox 无法让 Guard 通过查看文件已更改,除非您添加 -p 轮询参数。相反,它会在 Spork 已经运行时尝试运行所有内容,这会导致“无法启动 spork”错误。这也会导致测试速度变慢。现在,我将坚持在控制台中点击“输入”来运行所有测试。我不相信让 Guard 正常工作会节省在我的 Ruby 知识水平上编辑所有 gem 的时间。
标签: ruby rspec ruby-on-rails-4 guard spork