【问题标题】:In RSpec, what's the difference between before(:suite) and before(:all)?在 RSpec 中,before(:suite) 和 before(:all) 有什么区别?
【发布时间】:2013-03-16 04:50:17
【问题描述】:

The before and after hook documentation on Relish 只表明before(:suite)before(:all) 之前被调用。

我应该什么时候使用一个而不是另一个?

【问题讨论】:

    标签: rspec rspec2


    【解决方案1】:

    before(:all)RSpec.configure 块中定义时,它会在每个顶级示例组之前调用,而before(:suite) 代码块只调用一次。

    这是一个例子:

    RSpec.configure do |config|
      config.before(:all) { puts 'Before :all' }
      config.after(:all) { puts 'After :all' }
      config.before(:suite) { puts 'Before :suite' }
      config.after(:suite) { puts 'After :suite' }
    end
    
    describe 'spec1' do
      example 'spec1' do
        puts 'spec1'
      end
    end
    
    describe 'spec2' do
      example 'spec2' do
        puts 'spec2'
      end
    end
    

    输出:

    Before :suite
    Before :all
    spec1
    After :all
    Before :all
    spec2
    After :all
    After :suite
    

    【讨论】:

    • 我的问题是:“如果我使用before suite 加载一些种子数据,如管理员用户,在示例运行后不会清除数据吗?”
    • 不,它们会留下来,你必须手动处理它们。只有 before(:example) 在事务内部。
    • :suite 和 :context (= :all) 在事务之外。无论上下文嵌套有多深。
    【解决方案2】:

    你也可以使用 before(:suite) 在任何代码之前运行一段代码 运行示例组。这应该在 RSpec.configure 中声明

    http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks

    【讨论】:

    • 我正在观察一个在我之前工作的人如何误解了“这应该在RSpec.configure 中声明”并将他所有的预运行装置放入RSpec.configure - - 他们现在甚至使用--dry-run 运行。我被告知“不要碰任何东西!只添加新的测试”......
    • 这是 2013 年,反正答案可能无关紧要。
    猜你喜欢
    • 1970-01-01
    • 2011-11-11
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2014-05-27
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多