Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec - how to exclude spec files in subdirectory?

Say I have the following spec subdirectories:

lib
models
observers
workers

In the spec_helper.rb file, how do I tell rspec to exclude all spec files in the lib subdirectory?

Spork.prefork do

  ENV['RAILS_ENV'] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'

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

  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = false

    config.before(:suite) do
      DatabaseCleaner.clean_with :truncation
      DatabaseCleaner.strategy = :transaction
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
  end

end

Spork.each_run do
  FactoryGirl.reload
end

FYI - I'm using guard to auto-reload tests.

like image 717
keruilin Avatar asked Nov 03 '22 20:11

keruilin


2 Answers

Not sure how to exclude but you can include a list in guard like this:

guard 'rspec', :spec_paths => ['spec/models', 'spec/workers', 'spec/observers'] do
  # ...
end
like image 70
Tanzeeb Khalili Avatar answered Nov 09 '22 16:11

Tanzeeb Khalili


One solution is Exclusion Filters:

RSpec.configure do |c|
  # declare an exclusion filter
  c.filter_run_excluding :broken => true
end

describe "something" do
  it "does one thing" do
  end

  # tag example for exclusion by adding metadata
  it "does another thing", :broken => true do
  end
end

exclusion flag can be applied to describe and context too.

Also, this is useful config option:

RSpec.configure do |c|
  c.run_all_when_everything_filtered = true
end

So if everything in /lib is excluded, you will still be able to run specs manually with rspec spec/lib/

like image 34
vrybas Avatar answered Nov 09 '22 17:11

vrybas