1

我正在使用 spork 来加快我的 RSpec 测试,但是我无法让它在每次运行时重新加载过滤条件,例如config.filter_run_excluding :slow => true

不管我是否将这些过滤器放入Spork.each_run块中,它们似乎只在它第一次启动时才被加载,这很痛苦。

是否有可能让它们每次都被重新解释?

我的 spec_helper 文件:

require 'rubygems'
require 'spork'
require 'shoulda/integrations/rspec2'


Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shared/test_macros'
  require 'shared/custom_matchers'
  require "email_spec"

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :rspec
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.include(TestMacros)
  end
end

Spork.each_run do
  RSpec.configure do |config|
    # THESE ONLY SEEM TO BE INTERPRETED DURING PRE-FORK
    config.filter_run_excluding :slow => true
    config.filter_run :now => true
  end
end
4

2 回答 2

2

Spork.each_run在 prefork 期间没有运行,但代码已经加载到内存中。这意味着在您重新启动 spork 之前,您所做的任何编辑都不会被看到。Spork 在更改时不会自动重新加载 spec_helper.rb。

为了解决这个问题,您可以each_run要求或加载另一个文件,该文件使用所需的过滤器调用 RSpec.configure。

附带说明一下,如果您升级到 rspec 2.8,您将遇到一个错误,即 rspec 的过滤器被 spork 破坏:rspec-core #554。我相信无论您是将过滤器内联each_run还是放在外部文件中,都会发生这种情况。

于 2012-01-20T21:02:33.853 回答
0

尽管从技术上讲,这不是我提出的问题的答案,但我确实找到了解决方案。

我本质上想要做的是偶尔专注于几个测试,即使用config.filter_run :now => true,然后在这些测试通过时回退到运行所有测试。我发现您可以通过添加以下不言自明的配置行来做到这一点:

config.run_all_when_everything_filtered = true

后续each_run封锁spec_helper

RSpec.configure do |config|
  # restrict tests to a particular set
  config.filter_run_excluding :slow => true
  config.filter_run :now => true
  config.run_all_when_everything_filtered = true
end

这样做是在所有测试都不符合过滤条件时运行所有测试。即,如果您:now => true从所有测试块中删除,那么它们都会运行。一旦您再次添加它,只有标记的块将运行。

于 2011-11-12T13:02:33.943 回答