【问题标题】:Defined order just for one spec file仅为一个规范文件定义的顺序
【发布时间】:2014-01-03 05:53:57
【问题描述】:

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

config.order = 'random'

致我的 spec_helper

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

我修改了我的文件如下

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

  it'....'
  end
end

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

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

谢谢

【问题讨论】:

    标签: ruby rspec2


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我刚刚在 Rspec 2 中完成了这项工作 - 就我而言,我在一个规范文件中混合了有序和无序测试,因此我使用了以下内容:

      # 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
      

      【讨论】:

        猜你喜欢
        • 2020-06-22
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        • 1970-01-01
        • 2016-08-21
        • 1970-01-01
        相关资源
        最近更新 更多