仅为一个spec文件定义的顺序

时间:2014-01-03 05:53:57

标签: ruby rspec2

我更喜欢以随机顺序运行我的RSpec(2.14)规格,所以我添加了

config.order = 'random'

到我的spec_helper

但是,对于一个且只有一个文件,我需要按照它们的编写顺序运行它们。

我修改了我的文件,如下所示

describe ApiAuthentication do 
  RSpec.configure do |config|
    config.order = 'defined'
  end

  it'....'
  end
end

但它们仍以随机顺序执行。

有没有办法为1个文件指定定义的顺序?

谢谢

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我刚刚在Rspec 2中完成了这个 - 在我的情况下,我在一个spec文件中混合了有序和无序测试,所以我使用了以下内容:

# order by a number at start of the description (default 0) and then random
# note 'describe' groups always sort after 'it' examples
RSpec.configure do |config|
  config.order_groups_and_examples do |list|
    list.sort_by { |item| [item.description.sub(/^(\d*).*/, '\1').to_i, rand] }
  end
end

结合使用describe进行分组,这解决了我的要求(大多数测试我不关心顺序,但是我想按顺序或最后完成)。例如:

it "1: starts when asked to"
it "1: should do something"
it "waves happily"
it "snores loudly"
describe "9: at the end" do
  it "stops when asked to"
end

describe "4: some grouping" do
  it "does another thing"
  it "has weight"
end

describe "4: another group" do
  it "waddles like a duck"
  it "quacks like a duck"
end

按以下顺序运行:

# next two in random order
it "waves happily"
it "snores loudly"

# next two in random order
it "1: should do something"
it "1: starts when asked to"

# next two describe blocks in random order, and their examples within then in random order
describe "2: another group" do
  it "waddles like a duck"
  it "quacks like a duck"
end
describe "2: some grouping" do
  it "does another thing"
  it "has weight"
end

# NOTE: an it "9: something" example without a surrounding
#  describe block would run before all the describe blocks.

# this will run at the end
describe "9: at the end" do
  it "stops when asked to"
end