为什么Guard会在每次更改时运行所有RSpec规范?

时间:2013-05-03 13:55:34

标签: ruby-on-rails-3 rspec guard

此测试堆栈的行为不可靠。有时当我运行后卫时,它会使用guard-rspec gem充分执行,并且只会运行已更改的文件。至少,如果测试失败,它将停止使用该单个修改过的文件。

但如果该测试通过,所有赌注都将被取消。当单个文件发生更改时,它将继续运行 所有 测试。在一个大小合适的应用程序中,每次保存文件时都会看到所有测试运行,这是非常不切实际和令人沮丧的。

我的Guardfile如下。请注意,我必须更改它指向RSpec规范的位置。默认情况下,它认为您将拥有一个specs目录,并且您的所有规范都将混乱到该目录中。我不知道有谁测试那样,所以这是一个有趣的默认包括。它应该在specs下查找子目录。

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'spork', :cucumber => false, :rspec_env => { 'RAILS_ENV' => 'test' } do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch('config/environments/test.rb')
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb') { :rspec }
end

guard 'rspec', :version => 2, :cli => "--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 examples
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |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" }

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/features/#{m[1]}_spec.rb" }
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end

2 个答案:

答案 0 :(得分:3)

guard-rspec gem有一个参数,用于控制是否在成功测试后运行所有测试。它默认为true,但可以更改为false以获得您正在寻找的行为。

请参阅https://github.com/guard/guard-rspec

上的:all_after_pass选项

答案 1 :(得分:2)

为了便于参考,需要为每个保护块设置all_on_start: falseall_after_pass: false选项。我花了一段时间才弄清楚为什么我的行为方式相同。

相关问题