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

Note:

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.

Note:

: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.

Overloads:

  • #around(scope, &block) ⇒ void

    Parameters:

    • scope (Symbol)

      :example (defaults to :example) present for syntax parity with before and after, but :example/:each is the only supported value.

  • #around(scope, *conditions, &block) ⇒ void

    Parameters:

    • scope (Symbol)

      :example (defaults to :example) present for syntax parity with before and after, but :example/:each is the only supported value.

    • conditions (Array<Symbol>, Hash)

      constrains this hook to examples matching these conditions e.g. around(:example, :ui => true) { ... } will only run with examples or groups declared with :ui => true. Symbols will be transformed into hash entries with true values.

  • #around(conditions, &block) ⇒ void

    Parameters:

    • conditions (Hash)

      constrains this hook to examples matching these conditions e.g. around(:example, :ui => true) { ... } will only run with examples or groups declared with :ui => true.

Yields:



349
350
351
# File 'lib/rspec/core/hooks.rb', line 349

def around(*args, &block)
  hooks.register :prepend, :around, *args, &block
end