Skip to content

Instantly share code, notes, and snippets.

@jimeh
Created September 24, 2020 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimeh/621a9b58499266ae368848a992dcbc6c to your computer and use it in GitHub Desktop.
Save jimeh/621a9b58499266ae368848a992dcbc6c to your computer and use it in GitHub Desktop.

RSpec Hook Order

The following is a quick reference to exact order of hook execution in RSpec. The output from hook_spec.rb is:

describe: "a thing"
  -> before: all (top)
    -> around: before (top)
      -> before: each (top)
        -> example: "success" (top)
      -> after: each (top)
    -> around: after (top)
  result: example: "success"
    -> around: before (top)
      -> before: each (top)
        -> example: "failure" (top)
      -> after: each (top)
    -> around: after (top)
  result: example: "failures" (FAILED - 1)
    -> around: before (top)
      -> before: each (top)
        -> example: "exception" (top)
      -> after: each (top)
    -> around: after (top)
  result: example: "exception" (FAILED - 2)
  context: "when a thing"
    -> before: all (context)
    -> around: before (top)
      -> around: before (context)
      -> before: each (top)
        -> before: each (context)
          -> example: "success" (context)
        -> after: each (context)
      -> after: each (top)
      -> around: after (context)
    -> around: after (top)
    result: context example: "success"
    -> around: before (top)
      -> around: before (context)
      -> before: each (top)
        -> before: each (context)
          -> example: "failure" (context)
        -> after: each (context)
      -> after: each (top)
      -> around: after (context)
    -> around: after (top)
    result: context example: "failures" (FAILED - 3)
    -> around: before (top)
      -> around: before (context)
      -> before: each (top)
        -> before: each (context)
          -> example: "exception" (context)
        -> after: each (context)
      -> after: each (top)
      -> around: after (context)
    -> around: after (top)
    result: context example: "exception" (FAILED - 4)
    -> after: all (context)
  -> after: all (top)
# frozen_string_literal: true
RSpec.describe 'describe: "a thing"' do
before(:all) do
puts ' -> before: all (top)'
end
around do |example|
puts ' -> around: before (top)'
example.run
puts ' -> around: after (top)'
end
after(:all) do
puts ' -> after: all (top)'
end
before do
puts ' -> before: each (top)'
end
after do
puts ' -> after: each (top)'
end
it 'result: example: "success"' do
puts ' -> example: "success" (top)'
end
it 'result: example: "failures"' do
puts ' -> example: "failure" (top)'
expect([]).to include('nope')
end
it 'result: example: "exception"' do
puts ' -> example: "exception" (top)'
raise 'nope'
end
context 'context: "when a thing"' do
before(:all) do
puts ' -> before: all (context)'
end
around do |example|
puts ' -> around: before (context)'
example.run
puts ' -> around: after (context)'
end
after(:all) do
puts ' -> after: all (context)'
end
before do
puts ' -> before: each (context)'
end
after do
puts ' -> after: each (context)'
end
it 'result: context example: "success"' do
puts ' -> example: "success" (context)'
end
it 'result: context example: "failures"' do
puts ' -> example: "failure" (context)'
expect([]).to include('nope')
end
it 'result: context example: "exception"' do
puts ' -> example: "exception" (context)'
raise 'nope'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment