Skip to content

Commit

Permalink
Merge pull request #2661 from rspec/update-hook-order-docs
Browse files Browse the repository at this point in the history
Update docs for hooks specifying order of before/around/after
  • Loading branch information
JonRowe committed Sep 12, 2019
2 parents 25b10ef + 0058c8e commit 8a287bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions features/hooks/around_hooks.feature
Expand Up @@ -17,6 +17,9 @@ Feature: `around` hooks
**WARNING:** Mock frameworks are set up and torn down within the context of
running the example. You cannot interact with them directly in `around` hooks.

**WARNING:** `around` hooks will execute *after* any `before` hooks, and *before*
any `after` hooks regardless of the context they were defined in.

Scenario: Use the example as a proc within the block passed to `around()`
Given a file named "example_spec.rb" with:
"""ruby
Expand Down
3 changes: 3 additions & 0 deletions features/hooks/before_and_after_hooks.feature
Expand Up @@ -29,6 +29,9 @@ Feature: `before` and `after` hooks

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

**WARNING:** `around` hooks will execute *after* any `before` hooks, and *before*
any `after` hooks regardless of the context they were defined in.

Note: the `:example` and `:context` scopes are also available as `:each` and
`:all`, respectively. Use whichever you prefer.

Expand Down
12 changes: 10 additions & 2 deletions lib/rspec/core/hooks.rb
Expand Up @@ -60,7 +60,8 @@ module Hooks
# before(:example) # Declared in the current group.
#
# If more than one `before` is declared within any one scope, they are run
# in the order in which they are declared.
# in the order in which they are declared. Any `around` hooks will execute
# later than any `before` hook regardless of scope.
#
# ### Conditions
#
Expand Down Expand Up @@ -261,7 +262,8 @@ def prepend_before(*args, &block)
#
# This is the reverse of the order in which `before` hooks are run.
# Similarly, if more than one `after` is declared within any one scope,
# they are run in reverse order of that in which they are declared.
# they are run in reverse order of that in which they are declared. Also
# `around` hooks will all have run before any after hooks are invoked.
#
# @note The `:example` and `:context` scopes are also available as
# `:each` and `:all`, respectively. Use whichever you prefer.
Expand Down Expand Up @@ -329,6 +331,12 @@ def append_after(*args, &block)
# around(:example) {|ex| Database.transaction(&ex)}
# around(:example) {|ex| FakeFS(&ex)}
#
# ### Order
#
# All `around` hooks execute immediately surrounding an example, this means
# that all `before` hooks will have run and no `after` hooks will have run yet.
#
# They are not a synonym for `before`/`after`.
def around(*args, &block)
hooks.register :prepend, :around, *args, &block
end
Expand Down

0 comments on commit 8a287bd

Please sign in to comment.