Guard + Spork减慢了测试速度,没有接收--drb选项

时间:2014-03-02 23:11:23

标签: ruby rspec ruby-on-rails-4 guard spork

我正在通过Vagrant框(Ubuntu 32位,Rails 4.0.3,Ruby 2.1.0p0)开发Rails项目。

我刚刚尝试将Spork添加到我的项目中以加速我的测试(使用RSpec,Capybara),并且我看到测试速度明显变慢。如果我只运行“rspec。”,我所有的测试都会在5.83秒内完成。但是,当我通过“guard -c -p”运行后卫时,我保存了一个spec文件,并得到了26.08秒的时间。

注意:我必须运行“guard -p”来实际防范通过Vagrant在文件中运行我的测试。

当守卫开始运行测试时,它会显示args:

["--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盒子时速度要快得多。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

自Guard 2.5以来有一个新的--listen-on选项:https://github.com/guard/guard/releases/tag/v2.5.0

来自Guard's README

  

使用Listen的网络功能从网络接收文件更改事件。这对于在客户操作系统上触发本机文件系统事件时出现问题的虚拟机(例如Vagrant)非常有用。

     

建议用途:

     

在主机操作系统上,您需要使用listen脚本侦听文件系统事件并将其转发到VM:

     

$ listen -f 127.0.0.1:4000

     

请记住将VM配置为转发相应的端口,例如在Vagrantfile中:   config.vm.network :forwarded_port, guest: 4000, host: 4000

     

然后,在您的来宾操作系统上,收听网络事件,但请确保指定主机路径   $ bundle exec guard -o '10.0.2.2:4000' -w '/projects/myproject'