【问题标题】:RSpec - how to exclude spec files in subdirectory?RSpec - 如何排除子目录中的规范文件?
【发布时间】:2014-06-26 15:46:30
【问题描述】:

假设我有以下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

仅供参考 - 我正在使用警卫来自动重新加载测试。

【问题讨论】:

    标签: configuration rspec directory


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      一个解决方案是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/手动运行规范

      【讨论】:

        猜你喜欢
        • 2018-01-08
        • 2014-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多