如何运行单个TAGGED规范

时间:2012-08-08 12:00:44

标签: ruby-on-rails ruby rspec

我知道在文件中运行单个规范有多种方法。即 只要规范没有被标记(即非常慢......),这就像魅力一样。

rspec spec/models/event_spec.rb:311

这会运行标记为慢速的所有规格......

rspec -t slow spec/models/event_spec.rb

......但是大约需要两年时间才能完成,因为他们真的很慢,而且还有很多。

以下所有限制慢速规范运行的尝试最终都会运行指定文件中的所有未标记规范:

rspec -t slow -e "A describe block tagged as slow" spec/models/event_spec.rb
rspec -e "A describe block tagged as slow" spec/models/event_spec.rb
rspec -t slow spec/models/event_spec.rb:378 #the line number of the describe block
rspec spec/models/event_spec.rb:378 #the line number of the describe block
rspec -e "A single slow spec" -t slow spec/models/event_spec.rb
rspec -t slow -e "A single slow spec" -t slow spec/models/event_spec.rb
rspec -t slow spec/models/event_spec.rb:466 #a line number of a single slow spec
rspec spec/models/event_spec.rb:466 #a line number of a single slow spec
#It seems the order of the file/path and the options makes no difference..
rspec spec/models/event_spec.rb -t slow -e "A describe block tagged as slow"
rspec spec/models/event_spec.rb -e "A describe block tagged as slow"
rspec spec/models/event_spec.rb:378 -t slow #the line number of the describe block
rspec spec/models/event_spec.rb -e "A single slow spec" -t slow
rspec spec/models/event_spec.rb -t slow -e "A single slow spec"
rspec spec/models/event_spec.rb#466 -t slow #a line number of a single slow spec

这是不可能的,或者我做错了什么?

P.S。 Rspec版本2.10.1

更新

我没有提到在spec_helper中,Rspec(显然)被配置为默认情况下不运行慢速测试,并且如果一切都被过滤则运行所有测试。 (TBH,我认为这是默认行为,这就是我最初没有提到它的原因)

RSpec.configure do |config|
  config.mock_with(:rspec)
  config.expect_with(:rspec)
  config.filter_run_excluding(:slow => true)
  config.run_all_when_everything_filtered = true
end

这应该足以测试行为:

# -*- encoding : utf-8 -*-
require 'spec_helper'

describe 'Demonstrating tagging issue' do
  describe "some tests that take a very long time to run", slow: true do
    it "slow test 1" do
      puts "it should be slow"
    end
    it "shlow test 2" do
      puts "it should be slow too"
    end
  end
  it "normal test" do
    puts "it should be fast"
  end
end

所以,似乎如果我尝试指定按行号或示例名称运行的确切测试,那么它会忽略我尝试的所有努力并强制它更改配置的默认behvavi忽略慢速规范..因此一切过滤后,它会运行一切。

更新2

进一步说明我想要实现的目标。我有一个文件,有多个示例标记为慢。我不想触摸那些标签,我想运行这些慢速示例中的一个。因此,如果从我的spec_helper.rb

中注释掉这个设置,这一切都是小菜一碟
config.filter_run_excluding(:slow => true)

然后任何方法:指定行号,-e cmd行选项,焦点标记你想要使用的东西都可以使用。但是,我本以为可以实现这一目标,而不必使用配置。现在我很确定这实际上很可能是目前不可能的..

1 个答案:

答案 0 :(得分:0)

我在你的例子中做了一些调整,它可以按你的意愿工作,我希望。

spec/something_spec.rb:

# -*- encoding : utf-8 -*-
require 'spec_helper'

describe 'Demonstrating tagging issue' do
  describe "run differently slow" do
    it "is slow", slow: true do
      true.should be_true
    end

    it "is not so fast" do
      true.should be_true
    end

    it "normal test" do
      true.should be_true
    end
  end
end

终端:

╭─@krasimir.local /private/tmp/rtest ‹ruby-1.9.2@test› 
╰─ rspec -f d --tag true  
Run options: include {:focus=>true, :true=>true}

All examples were filtered out; ignoring {:focus=>true, :true=>true}

Demonstrating tagging issue
  run differently slow
    is slow
    is not so fast
    normal test

Finished in 0.00527 seconds
3 examples, 0 failures

Randomized with seed 57868

╭─@krasimir.local /private/tmp/rtest ‹ruby-1.9.2@test› 
╰─ rspec -f d --tag slow
Run options: include {:focus=>true, :slow=>true}

Demonstrating tagging issue
  run differently slow
    is slow

Finished in 0.00772 seconds
1 example, 0 failures

我用过:

  • Ruby:ruby 1.9.2p320(2012-04-20修订版35421)[x86_64-darwin12.0.0]
  • RSpec:2.11.1
相关问题