RSpec - 如何排除子目录中的spec文件?

时间:2012-07-14 15:34:43

标签: configuration rspec directory

说我有以下spec子目录:

lib
models
observers
workers

在spec_helper.rb文件中,如何告诉rspec排除lib子目录中的所有spec文件?

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

仅供参考 - 我正在使用guard进行自动重载测试。

2 个答案:

答案 0 :(得分:1)

不确定如何排除,但您可以像这样在防守中包含一个列表:

guard 'rspec', :spec_paths => ['spec/models', 'spec/workers', 'spec/observers'] do
  # ...
end

答案 1 :(得分:1)

一个解决方案是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

排除标记也可以应用于describecontext

此外,this是有用的配置选项:

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

因此,如果排除/lib中的所有内容,您仍然可以使用 使用rspec spec/lib/

手动运行规范