Search code examples
ruby-on-railsrspecspork

How do I get spork to respect my run filters?


I'm using spork to speed up my RSpec tests however I can't get it to reload the filter conditions on each run e.g. config.filter_run_excluding :slow => true

Regardless of whether I put these filters into the Spork.each_run block or not they only seem to be loaded when it first starts up which is a pain.

Is it possible to get them to be re-interpreted every time?

My spec_helper file:

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

Solution

  • Although it isn't technically the answer to the question I asked I did find a solution to this.

    What I essentially wanted to do was to occasionally focus on a couple of tets i.e. use config.filter_run :now => true and then fall back to running all tests when those passed. I found out that you can do this by adding the following self-explanatory config line:

    config.run_all_when_everything_filtered = true
    

    Subsequent each_run block in 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
    

    What this does is run all of the tests when none of them are eligible via filters. I.e. if you remove :now => true from all test blocks then they'll all run. As soon as you add it back in again only the flagged block will run.