在RSpec中,什么';那之前(:suite)和之前(:all)的区别是什么?

在RSpec中,什么';那之前(:suite)和之前(:all)的区别是什么?,rspec,rspec2,Rspec,Rspec2,仅显示在(:suite)之前(:all)之前调用before(:suite) 什么时候我应该使用一个而不是另一个 您还可以使用before(:suite)在任何 运行示例组。这应该在RSpec.configure中声明 当在RSpec.configure块中定义了before(:all)时,将在每个顶级示例组之前调用它,而before(:suite)代码块只调用一次 下面是一个例子: RSpec.configure do |config| config.before(:all) { puts

仅显示在(:suite)之前(:all)之前调用
before(:suite)

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

您还可以使用before(:suite)在任何 运行示例组。这应该在RSpec.configure中声明

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

我的问题是:“如果我在suite之前使用
加载一些种子数据,比如admin User,那么在示例运行之后,这些数据会被清除吗?”不会,它们会留下来,您必须手动处理它们。仅在(:example)之前是内部事务。:suite和:context(:all)是外部事务。不管上下文嵌套得有多深。我观察到一个在我之前工作的人是如何误解“这应该在
RSpec.configure
中声明的”,并将所有的预运行装置都放入
RSpec.configure
——现在即使在
--干运行时也可以运行。我被告知“不要碰任何东西!只添加新的测试”……这是2013年,答案可能与此无关。