使用 RSpec,如何编写捕获嵌套目录中的 _spec 文件的表达式?

With RSpec, how do I write an expression that captures _spec files in nested directories?

我正在使用 Rails 6 和 RSpec 3.10。我有以下目录结构

spec/models/my_object_spec.rb
spec/models/module_name/product_spec.rb

我运行 使用类似于下面的命令进行测试

bundle exec rspec --color --require spec_helper --format RspecJunitFormatter --out $CIRCLE_TEST_REPORTS/rspec/rspec.xml --format progress $(circleci tests glob spec/**/*_spec.rb | circleci tests split --split-by=timings)

问题是正则表达式“spec/**/*_spec.rb”似乎只运行文件“spec/models/my_object_spec.rb”,然而,另一个文件在“模型”的子目录中没有得到 运行。有没有办法编写正则表达式,使其捕获所有以“_spec”结尾的文件,而不管它们在哪个子目录中?

glob 不是正则表达式,因此您需要启用 shell-option globstar 才能递归子目录。

在测试调用之前添加行 - run: shopt -s globstar 将不起作用,因为 CircleCI 运行 中的每个步骤都在其自己的 shell 实例中。您需要在 运行 测试的同一步骤中设置 shell-opt。像这样:

- run:
  command: |
    shopt -s globstar
    bundle exec rspec --color --require spec_helper --format RspecJunitFormatter --out $CIRCLE_TEST_REPORTS/rspec/rspec.xml --format progress $(circleci tests glob spec/**/*_spec.rb | circleci tests split --split-by=timings)