我正在通过Vagrant开发一个Rails项目(Ubuntu 32位,Rails 4.0.3,Ruby2.1.0p0)。
为了加快测试速度(使用RSpec,Capybara),我刚刚尝试了在我的项目中添加Spork,现在看到的测试要慢得多。如果我只运行"rspec .",我的所有测试都会在5.83秒内通过。但是,当我通过“-c -p”运行保护程序时,我保存了一个规范文件,时间为26.08秒。
注意:我必须运行“守护-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"]...我看到“--格式”列出了两次,而"--drb“根本没有出现。
这是我的守护文件
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时。有什么想法吗?
发布于 2014-03-05 21:49:55
从Guid2.5开始有一个新的--listen-on选项:https://github.com/guard/guard/releases/tag/v2.5.0
来自警卫自述
使用Listen的网络功能从网络接收文件更改事件。这对于虚拟机(例如Vagrant)非常有用,因为虚拟机在客户操作系统上触发本机文件系统事件时有问题。 建议使用: 在主机操作系统上,您需要侦听文件系统事件,并使用侦听脚本将它们转发到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'。
https://stackoverflow.com/questions/22134969
复制相似问题