Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 8a287bd

Browse files
authored
Merge pull request #2661 from rspec/update-hook-order-docs
Update docs for hooks specifying order of before/around/after
2 parents 25b10ef + 0058c8e commit 8a287bd

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

features/hooks/around_hooks.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Feature: `around` hooks
1717
**WARNING:** Mock frameworks are set up and torn down within the context of
1818
running the example. You cannot interact with them directly in `around` hooks.
1919

20+
**WARNING:** `around` hooks will execute *after* any `before` hooks, and *before*
21+
any `after` hooks regardless of the context they were defined in.
22+
2023
Scenario: Use the example as a proc within the block passed to `around()`
2124
Given a file named "example_spec.rb" with:
2225
"""ruby

features/hooks/before_and_after_hooks.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Feature: `before` and `after` hooks
2929

3030
**WARNING:** Mocks are only supported in `before(:example)`.
3131

32+
**WARNING:** `around` hooks will execute *after* any `before` hooks, and *before*
33+
any `after` hooks regardless of the context they were defined in.
34+
3235
Note: the `:example` and `:context` scopes are also available as `:each` and
3336
`:all`, respectively. Use whichever you prefer.
3437

lib/rspec/core/hooks.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ module Hooks
6060
# before(:example) # Declared in the current group.
6161
#
6262
# If more than one `before` is declared within any one scope, they are run
63-
# in the order in which they are declared.
63+
# in the order in which they are declared. Any `around` hooks will execute
64+
# later than any `before` hook regardless of scope.
6465
#
6566
# ### Conditions
6667
#
@@ -261,7 +262,8 @@ def prepend_before(*args, &block)
261262
#
262263
# This is the reverse of the order in which `before` hooks are run.
263264
# Similarly, if more than one `after` is declared within any one scope,
264-
# they are run in reverse order of that in which they are declared.
265+
# they are run in reverse order of that in which they are declared. Also
266+
# `around` hooks will all have run before any after hooks are invoked.
265267
#
266268
# @note The `:example` and `:context` scopes are also available as
267269
# `:each` and `:all`, respectively. Use whichever you prefer.
@@ -329,6 +331,12 @@ def append_after(*args, &block)
329331
# around(:example) {|ex| Database.transaction(&ex)}
330332
# around(:example) {|ex| FakeFS(&ex)}
331333
#
334+
# ### Order
335+
#
336+
# All `around` hooks execute immediately surrounding an example, this means
337+
# that all `before` hooks will have run and no `after` hooks will have run yet.
338+
#
339+
# They are not a synonym for `before`/`after`.
332340
def around(*args, &block)
333341
hooks.register :prepend, :around, *args, &block
334342
end

0 commit comments

Comments
 (0)