Method: RSpec::Core::Hooks#after

Defined in:
lib/rspec/core/hooks.rb

#after(&block) ⇒ void #after(scope, &block) ⇒ void #after(scope, *conditions, &block) ⇒ void #after(conditions, &block) ⇒ void Also known as: prepend_after

Note:

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

Note:

The :suite scope is only supported for hooks registered on RSpec.configuration since they exist independently of any example or example group.

Declare a block of code to be run after each example (using :example) or once after all examples n the context (using :context). See #before for more information about ordering.

Exceptions

after hooks are guaranteed to run even when there are exceptions in before hooks or examples. When an exception is raised in an after block, the exception is captured for later reporting, and subsequent after blocks are run.

Order

after hooks are stored in three scopes, which are run in order: :example, :context, and :suite. They can also be declared in several different places: RSpec.configure, a parent group, the current group. They are run in the following order:

after(:example) # Declared in the current group.
after(:example) # Declared in a parent group.
after(:example) # Declared in RSpec.configure.
after(:context) # Declared in the current group.
after(:context) # Declared in a parent group.
after(:context) # Declared in RSpec.configure.
after(:suite)   # Declared in RSpec.configure.

This is the reverse of the order in which before hooks are run. Similarly, if more than one after is declared within any example group, they are run in reverse order of that in which they are declared. Also around hooks will run after any after example hooks are invoked but before any after context hooks.

Overloads:

  • #after(scope, &block) ⇒ void

    Parameters:

    • scope (Symbol)

      :example, :context, or :suite (defaults to :example)

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

    Parameters:

    • scope (Symbol)

      :example, :context, or :suite (defaults to :example)

    • conditions (Array<Symbol>, Hash)

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

  • #after(conditions, &block) ⇒ void

    Parameters:

    • conditions (Hash)

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

See Also:

[View source]

277
278
279
# File 'lib/rspec/core/hooks.rb', line 277

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