1

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

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

这是不可能的还是我做错了什么?

PS 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

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

更新 2

进一步说明我想要实现的目标。我有一个文件,其中有多个示例标记为慢。我不想碰这些标签,我想运行一个缓慢的例子。所以如果从我的 spec_helper.rb 中注释掉这个设置,这一切都是小菜一碟

config.filter_run_excluding(:slow => true)

然后任何方法:指定行号,-e cmd 行选项,焦点标记你想使用的任何东西都可以工作。但是,我原以为无需修改配置就可以实现这一目标。现在我越来越确定这实际上是目前最不可能的..

4

1 回答 1

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
于 2012-08-09T21:41:05.490 回答