Module: RSpec::Core::Hooks

Included in:
Configuration, ExampleGroup, SharedContext, World
Defined in:
lib/rspec/core/hooks.rb

Defined Under Namespace

Classes: AfterHook, AfterHooks, AroundHook, AroundHooks, BeforeHook, BeforeHooks, Hook, HookCollection

Instance Method Summary collapse

Instance Method Details

#after(*args, &block) ⇒ Object



97
98
99
100
# File 'lib/rspec/core/hooks.rb', line 97

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

#around(*args, &block) ⇒ Object



102
103
104
105
# File 'lib/rspec/core/hooks.rb', line 102

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

#before(*args, &block) ⇒ Object



92
93
94
95
# File 'lib/rspec/core/hooks.rb', line 92

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

#find_hook(hook, scope, example_group_class, example = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rspec/core/hooks.rb', line 123

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

#hooksObject



84
85
86
87
88
89
90
# File 'lib/rspec/core/hooks.rb', line 84

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

#run_hook(hook, scope, example_group_instance = nil) ⇒ Object

Runs all of the blocks stored with the hook in the context of the example. If no example is provided, just calls the hook directly.



109
110
111
# File 'lib/rspec/core/hooks.rb', line 109

def run_hook(hook, scope, example_group_instance=nil)
  hooks[hook][scope].run_all(example_group_instance)
end

#run_hook!(hook, scope, example_group_instance) ⇒ Object

Just like run_hook, except it removes the blocks as it evalutes them, ensuring that they will only be run once.



115
116
117
# File 'lib/rspec/core/hooks.rb', line 115

def run_hook!(hook, scope, example_group_instance)
  hooks[hook][scope].run_all!(example_group_instance)
end

#run_hook_filtered(hook, scope, group, example_group_instance, example = nil) ⇒ Object



119
120
121
# File 'lib/rspec/core/hooks.rb', line 119

def run_hook_filtered(hook, scope, group, example_group_instance, example = nil)
  find_hook(hook, scope, group, example).run_all(example_group_instance)
end