Method: RSpec::Core::Hooks#around
- Defined in:
- lib/rspec/core/hooks.rb
#around(&block) ⇒ void #around(scope, &block) ⇒ void #around(scope, *conditions, &block) ⇒ void #around(conditions, &block) ⇒ void
the syntax of around
is similar to that of before
and after
but the semantics are quite different. before
and after
hooks are
run in the context of the examples with which they are associated,
whereas around
hooks are actually responsible for running the
examples. Consequently, around
hooks do not have direct access to
resources that are made available within the examples and their
associated before
and after
hooks.
:example
/:each
is the only supported scope.
Declare a block of code, parts of which will be run before and parts after the example. It is your responsibility to run the example:
around(:example) do |ex|
# Do some stuff before.
ex.run
# Do some stuff after.
end
The yielded example aliases run
with call
, which lets you treat it
like a Proc
. This is especially handy when working with libraries
that manage their own setup and teardown using a block or proc syntax,
e.g.
around(:example) {|ex| Database.transaction(&ex)}
around(:example) {|ex| FakeFS(&ex)}
Order
The around
hooks execute surrounding an example and its hooks.
This means after any before
context hooks, but before any before
example hooks, and similarly after any after
example hooks but before
any after
context hooks.
They are not a synonym for before
/after
.
349 350 351 |
# File 'lib/rspec/core/hooks.rb', line 349 def around(*args, &block) hooks.register :prepend, :around, *args, &block end |