2014-01-15 39 views
36

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

rspec spec/requests/my_favorite_spec.rb:100 

这将在管线100中my_favorite_spec.rb只运行单一的规范。

我试着输入上面的警卫控制台,但它只是运行所有的测试,就像我刚刚按下输入。在控制台中是否有另一种语法来运行单个规范?

回答

47

你必须论证你的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

+0

这不正是我需要的,并且链接了也有帮助。谢谢! – eirikir

+0

这有效,但是当我从'it'块中删除':focus => true'时,'guard'说''所有的例子都被过滤掉了'。显然这不能满足明显的工作流程。如果没有':focus'子句出现,完整的解决方案将运行所有示例。 –

+3

好的,当''focus''没有':focus'子句时,通过'RSpec.configure'中的'config.run_all_when_everything_filtered = true',你可以运行所有的规格。这里是[文档](https://relishapp.com/rspec/rspec-core/v/3-0/docs/configuration/run-all-when-everything-filtered)。 –

3

我想你可以添加“重点:真正的”选项,供您要运行的规范,像

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

然后保存文件和后卫只运行集中测试

时你完成你只是删除“重点:真”

相关问题