如何为rspec运行单一测试?

时间:2014-01-15 01:16:39

标签: ruby-on-rails ruby rspec guard

我使用guard-rspec在我的文件更改时自动运行必要的rspec测试,我喜欢它的工作原理。但是,当我调试具有多个测试的文件时,有时我只想重新运行单个测试。例如,使用命令行中的rspec:

rspec spec/requests/my_favorite_spec.rb:100

这将仅运行my_favorite_spec.rb中第100行的单个规范。

我尝试将上述内容输入到防护控制台,但它只是运行了所有测试,就像我刚按下回车一样。防护控制台中是否有另一种语法来运行单个规范?

3 个答案:

答案 0 :(得分:49)

您必须将spec/spec_helper.rb文件置为参与:focus => true语句。

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

然后你可以使用

it 'does something', :focus => true do
  //your spec
end

describe "something", :focus => true do
  before do
     sign in
     visit page
  end

  it { does something }
  it { does something else }
end

如果您打算采用这种方法,您可能还希望确保在任何地方没有:focus => true的情况下运行所有​​规范,使用documented approach

RSpec.configure do |config|
  config.run_all_when_everything_filtered = true
end

你可以用过滤器做很多事情;您可能需要查看此页面:https://relishapp.com/rspec/rspec-core/v/3-0/docs/filtering

答案 1 :(得分:4)

您可以在Guardfile中使用failed_mode: :focus,如下所述:https://github.com/guard/guard-rspec#list-of-available-options

答案 2 :(得分:3)

我认为您可以为要运行的规范添加“focus:true”选项,例如

it 'does something', focus: true do
  //your spec
end

然后你保存文件,后卫只运行那个重点测试

完成后,只需删除“focus:true”

即可
相关问题