Module: Minitest::Hooks::ClassMethods

Defined in:
lib/minitest/hooks.rb

Constant Summary collapse

NEW =

Object used to get an empty new instance, as new by default will return a dup of the singleton instance.

Object.new.freeze

Instance Method Summary collapse

Instance Method Details

#after(type = nil, &block) ⇒ Object

If type is :all, set the after_all hook instead of the after hook.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/minitest/hooks.rb', line 91

def after(type=nil, &block)
  if type == :all
    (defined?(@hooks_module) ? @hooks_module : self).send(:define_method, :after_all) do
      instance_exec(&block)
      super()
    end
    nil
  else
    super
  end
end

#before(type = nil, &block) ⇒ Object

If type is :all, set the before_all hook instead of the before hook.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/minitest/hooks.rb', line 78

def before(type=nil, &block)
  if type == :all
    (defined?(@hooks_module) ? @hooks_module : self).send(:define_method, :before_all) do
      super()
      instance_exec(&block)
    end
    nil
  else
    super
  end
end

#new(name) ⇒ Object

Unless name is NEW, return a dup singleton instance.



51
52
53
54
55
56
57
58
59
60
# File 'lib/minitest/hooks.rb', line 51

def new(name)
  if name.equal?(NEW)
    return super
  end

  instance = @instance.dup
  instance.name = name
  instance.failures = []
  instance
end

#run(reporter, options = {}) ⇒ Object

When running the specs in the class, first create a singleton instance, the singleton is used to implement around_all/before_all/after_all hooks, and each spec will run as a dup of the singleton instance.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/minitest/hooks.rb', line 65

def run(reporter, options={})
  r = nil
  @instance = new(NEW)

  @instance.around_all do
    @instance.before_all
    r = super
    @instance.after_all
  end
  r
end