module RSpec::Core::Hooks

def after(*args, &block)

Other tags:
    See: Configuration -
    See: SharedExampleGroup -
    See: SharedContext -
    See: ExampleGroup -
    See: #around -
    See: #before -

Parameters:
  • conditions (Hash) --
  • scope (Symbol) -- `:each`, `:all`, or `:suite` (defaults to `:each`)

Overloads:
  • after(conditions, &block)
  • after(scope, conditions, &block)
  • after(scope, &block)
  • after(&block)

Other tags:
    Api: - public
def after(*args, &block)
  scope, options = scope_and_options_from(*args)
  hooks[:after][scope] << AfterHook.new(options, &block)
end

def around(*args, &block)

Other tags:
    Note: - `:each` is the only supported scope.
    Note: - the syntax of `around` is similar to that of `before` and `after`

Other tags:
    Yield: - the example to run

Parameters:
  • conditions (Hash) --
  • scope (Symbol) -- `:each` (defaults to `:each`)

Overloads:
  • around(conditions, &block)
  • around(scope, conditions, &block)
  • around(scope, &block)
  • around(&block)

Other tags:
    Api: - public
def around(*args, &block)
  scope, options = scope_and_options_from(*args)
  hooks[:around][scope] << AroundHook.new(options, &block)
end

def before(*args, &block)

Other tags:
    Example: before(:all) declared in an [ExampleGroup](ExampleGroup) -
    Example: before(:each) declared in an [ExampleGroup](ExampleGroup) -

Other tags:
    See: Configuration -
    See: SharedExampleGroup -
    See: SharedContext -
    See: ExampleGroup -
    See: #around -
    See: #after -

Parameters:
  • conditions (Hash) --
  • scope (Symbol) -- `:each`, `:all`, or `:suite` (defaults to `:each`)

Overloads:
  • before(conditions, &block)
  • before(scope, conditions, &block)
  • before(scope, &block)
  • before(&block)

Other tags:
    Api: - public
def before(*args, &block)
  scope, options = scope_and_options_from(*args)
  hooks[:before][scope] << BeforeHook.new(options, &block)
end

def find_hook(hook, scope, example_group_class, example = nil)

Other tags:
    Private: -
def find_hook(hook, scope, example_group_class, example = nil)
  found_hooks = hooks[hook][scope].find_hooks_for(example || example_group_class)
  # ensure we don't re-run :all hooks that were applied to any of the parent groups
  if scope == :all
    super_klass = example_group_class.superclass
    while super_klass != RSpec::Core::ExampleGroup
      found_hooks = found_hooks.without_hooks_for(super_klass)
      super_klass = super_klass.superclass
    end
  end
  found_hooks
end

def hooks

Other tags:
    Private: -
def hooks
  @hooks ||= {
    :around => { :each => AroundHooks.new },
    :before => { :each => BeforeHooks.new, :all => BeforeHooks.new, :suite => BeforeHooks.new },
    :after => { :each => AfterHooks.new, :all => AfterHooks.new, :suite => AfterHooks.new }
  }
end

def run_hook(hook, scope, example_group_instance=nil)

Other tags:
    Private: -
def run_hook(hook, scope, example_group_instance=nil)
  hooks[hook][scope].run_all(example_group_instance)
end

def run_hook!(hook, scope, example_group_instance)

Other tags:
    Private: -
def run_hook!(hook, scope, example_group_instance)
  hooks[hook][scope].run_all!(example_group_instance)
end

def run_hook_filtered(hook, scope, group, example_group_instance, example = nil)

Other tags:
    Private: -
def run_hook_filtered(hook, scope, group, example_group_instance, example = nil)
  find_hook(hook, scope, group, example).run_all(example_group_instance)
end

def scope_and_options_from(*args)

def scope_and_options_from(*args)
  scope = if [:each, :all, :suite].include?(args.first)
    args.shift
  elsif args.any? { |a| a.is_a?(Symbol) }
    raise ArgumentError.new("You must explicitly give a scope (:each, :all, or :suite) when using symbols as metadata for a hook.")
  else
    :each
  end
  options = build_metadata_hash_from(args)
  return scope, options
end