You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用--pattern与--exclude-pattern参数时Rspec测试执行异常

RSpec 排除指定子目录测试的解决方法

你遇到的问题是用--pattern和--exclude-pattern无法正确排除spec/workers/study_workers/下的所有测试,核心原因是原排除模式没有递归匹配该子目录下的所有层级文件。

问题分析

原命令中的--exclude-pattern "spec/workers/study_workers/*_spec.rb"仅能匹配study_workers目录下直接存放的_spec.rb文件,若该目录下还有嵌套子目录,里面的测试文件不会被排除。

正确命令

只需修改排除模式,加入**实现递归匹配:

bundle exec rspec --pattern "spec/workers/**/*_spec.rb" --exclude-pattern "spec/workers/study_workers/**/*_spec.rb"

也可以简化写法,直接指定spec/workers/作为测试根目录,再配合递归排除模式:

bundle exec rspec spec/workers/ --exclude-pattern "spec/workers/study_workers/**/*_spec.rb"

验证方法

执行命令前,可先加上--dry-run参数预览要运行的测试文件,确认排除规则生效:

bundle exec rspec --dry-run --pattern "spec/workers/**/*_spec.rb" --exclude-pattern "spec/workers/study_workers/**/*_spec.rb"

内容的提问来源于stack exchange,提问作者Rodrigo Martins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 03:43:34