Rspec: around(:all) and around(:each) hook execution order
Summary
around(:suite)
does not exist.around(:all)
runs afterbefore(:all)
and beforeafter(:all)
.around(:each)
runs beforebefore(:each)
and afterafter(:each)
.
As this is not 100% obvious (and not yet documented) it is written down in this card. In RSpec 3 :each
has the alias :example
and :all
the alias :context
.
Example
CopyRSpec.configure do |config| config.before(:suite) { puts 'BEFORE :suite' } config.after(:suite) { puts 'AFTER :suite' } end describe 'order of hook execution' do around(:all) do |each| puts 'AROUND BEFORE :all' each.run puts 'AROUND AFTER :all' end around(:each) do |each| puts 'AROUND BEFORE :each' each.run puts 'AROUND AFTER :each' end before(:all) { puts 'BEFORE :all' } before(:each) { puts 'BEFORE :each' } after(:each) { puts 'AFTER :each' } after(:all) { puts 'AFTER :all' } it { expect(true).to be(true) } end
CopyBEFORE :suite BEFORE :all AROUND BEFORE :all AROUND BEFORE :each BEFORE :each . # our each runs here AFTER :each AROUND AFTER :each AROUND AFTER :all AFTER :all AFTER :suite
Further around hook related problems
- Cucumber pitfall: "Around" does not apply to your "Background" steps
- How to set up database_cleaner for Rails with Cucumber and RSpec
- Before all in database transactions will fail
Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).