在RSpec中,before(:suite)和before(:all)之间有什么区别?

Mar*_*off 39 rspec rspec2

关于Relish的前后钩子文档仅显示before(:suite)之前调用的before(:all).

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

Leo*_*Leo 70

当before(:all)在RSpec.configure块中定义a时,它在每个顶级示例组之前被调用,而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
Run Code Online (Sandbox Code Playgroud)

输出:

Before :suite
Before :all
spec1
After :all
Before :all
spec2
After :all
After :suite
Run Code Online (Sandbox Code Playgroud)