2014-03-02 40 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'

相關問題