2014-03-02 42 views
1

我正在通过Vagrant框(Ubuntu 32位,Rails 4.0.3,Ruby 2.1.0p0)开发Rails项目。Guard + Spork减慢测试,未收到-drb选项

我刚刚尝试将Spork添加到我的项目中以加快我的测试(使用RSpec,Capybara),并且看到显着较慢的测试。如果我只运行“rspec”,我的所有测试通过5.83秒。但是,当我通过“guard -c -p”跑卫队时,我保存了一个我的规格文件,我得到了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. 

我m不知道为什么安装Spork后需要这么长时间,特别是当rspec通过相同的Vagrant盒时速度更快。有任何想法吗?

+0

如果我删除了-p标志,测试速度可达约5秒(只有12测试,以便远,但它通过Vagrant运行,所以也许我会看到速度随着时间的推移而增加)。但是,我必须在运行Guard的控制台窗口中手动按下'enter',它将运行所有测试,而不是从文件更改中运行。 –

+0

我在这里找到了一个讨论: https://github.com/guard/guard-rspec/issues/183 基本上,问题似乎与VirtualBox不能让Guard通过查看文件已经改变,除非您添加-p轮询参数。相反,它试图在Spork已经运行时运行一切,这会导致“无法启动spork”错误。这也导致了较慢的测试。 现在,我会坚持只在命令行中输入'enter'来运行所有测试。我不相信通过让Guard工作正常,可以节省我在Ruby知识水平上编辑所有宝石的时间。 –

回答

1

有自卫队2.5新--listen-on选项:https://github.com/guard/guard/releases/tag/v2.5.0

Guard's README

Use Listen's network functionality to receive file change events from the network. This is most useful for virtual machines (e.g. Vagrant) which have problems firing native filesystem events on the guest OS.

Suggested use:

On the host OS, you need to listen to filesystem events and forward them to your VM using the listen script:

$ listen -f 127.0.0.1:4000

Remember to configure your VM to forward the appropriate ports, e.g. in Vagrantfile: config.vm.network :forwarded_port, guest: 4000, host: 4000

Then, on your guest OS, listen to the network events but ensure you specify the host path $ bundle exec guard -o '10.0.2.2:4000' -w '/projects/myproject'

相关问题