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

Update docs for hooks specifying order of before/around/after #2661

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions features/hooks/around_hooks.feature
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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