The before and after hook documentation on Relish only shows that before(:suite)
is called prior to before(:all)
.
When should I use one over the other?
The before and after hook documentation on Relish only shows that before(:suite)
is called prior to before(:all)
.
When should I use one over the other?
When a before(:all)
is defined in the RSpec.configure
block it is called before each top level example group, while a before(:suite)
code block is only called once.
Here's an example:
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
Output:
Before :suite
Before :all
spec1
After :all
Before :all
spec2
After :all
After :suite
You can also use before(:suite) to run a block of code before any example groups are run. This should be declared in RSpec.configure
http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks