【发布时间】:2013-03-16 04:50:17
【问题描述】:
The before and after hook documentation on Relish 只表明before(:suite) 在before(:all) 之前被调用。
我应该什么时候使用一个而不是另一个?
【问题讨论】:
The before and after hook documentation on Relish 只表明before(:suite) 在before(:all) 之前被调用。
我应该什么时候使用一个而不是另一个?
【问题讨论】:
当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(:suite) 在任何代码之前运行一段代码 运行示例组。这应该在 RSpec.configure 中声明
http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks
【讨论】:
RSpec.configure 中声明”并将他所有的预运行装置放入RSpec.configure - - 他们现在甚至使用--dry-run 运行。我被告知“不要碰任何东西!只添加新的测试”......