首页 > 解决方案 > 配置 rspec 以排除文件夹,除非它被明确指定给 rspec 运行

问题描述

我想从 rspec 测试中排除一个文件夹,除非它明确地作为命令行参数运行。例如

RSpec.configure do |config|

  unless rspec_args.any? { |arg| =~ /shared/ } # do not explicitly run shared folder
    config.exclude_pattern = 'shared/**/*_spec.rb' 
  end

end

我正在使用 rspec v3.9

我有两个问题:

  1. 配置似乎不会从测试中排除共享文件夹以运行exlude_patternconfig.exclude_pattern = 'shared/**/*_spec.rb'但是当我运行rspec --exclude-pattern shared/**/*_spec.rb共享文件夹时,会从测试套件中排除

  2. 如何从代码中的命令行获取传递给 rspec 的参数?

标签: rspecconfigurationrspec3

解决方案


我能够通过以下配置实现这一点:

# .rspec
--require spec_helper

# spec_helper.rb
RSpec.configure do |config|
  config.exclude_pattern = 'shared/**/*_spec.rb' 
end

推荐阅读