如何在 Rspec 中仅运行特定测试?

问题描述

:focus您可以使用hash 属性标记示例。例如,

# spec/foo_spec.rb
RSpec.describe Foo do
  it 'is never executed' do
    raise "never reached"
  end

  it 'runs this spec', focus: true do
    expect(1).to eq(1)
  end
end



rspec --tag focus spec/foo_spec.rb

更多信息在GitHub 上。(谁有更好的链接,请指教)

(更新)

RSpec 现在relishapp.com 上有很好的记录。有关详细信息,请参阅--tag 选项部分。

从 v2.6 开始,这种标签可以通过包含配置选项来更简单地表示treat_symbols_as_metadata_keys_with_true_values,它允许您执行以下操作:

describe "Awesome feature", :awesome do

where:awesome被视为:awesome => true.

此外,请参阅此答案以了解如何配置 RSpec以自动运行“重点”测试。这对Guard尤其有效。

确保在您的配置中配置了 RSpecspec_helper.rb以注意focus

RSpec.configure do |config|
  config.filter_run focus: true
  config.run_all_when_everything_filtered = true
end

然后在您的规范中,添加focus: true为参数:

it 'can do so and so', focus: true do
  # This is the only test that will run
end

您还可以通过更改itfit(或使用 排除测试xit)来集中测试,如下所示:

fit 'can do so and so' do
  # This is the only test that will run
end

解决方法

我认为有一种方法可以只运行具有给定标签的测试。有人知道吗?

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...