Module: Minitest::Hooks::ClassMethods

Defined in:
lib/minitest/hooks/test.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.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/minitest/hooks/test.rb', line 123

def after(type=nil, &block)
  if type == :all
   define_method(:after_all) do
      instance_exec(&block)
      super()
    end
    nil
  else
    super
  end
end

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

If type is :all, set the around_all hook, otherwise set the around hook.



104
105
106
107
# File 'lib/minitest/hooks/test.rb', line 104

def around(type=nil, &block)
  meth = type == :all ? :around_all : :around
  define_method(meth, &block)
end

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

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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/minitest/hooks/test.rb', line 110

def before(type=nil, &block)
  if type == :all
   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.



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

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

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

#with_info_handler(reporter, &block) ⇒ 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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/minitest/hooks/test.rb', line 64

def with_info_handler(reporter, &block)
  @instance = new(NEW)
  @instance.time = 0
  @instance.name = "around_all"
 
  begin
    @instance.around_all do
      begin
        @instance.capture_exceptions do
          @instance.name = "before_all"
          @instance.before_all
        end

        if @instance.failure
          failed = true
          _record_minitest_hooks_error(reporter, @instance)
        else
          super(reporter, &block)
        end
      ensure
        @instance.capture_exceptions do
          @instance.name = "after_all" unless failed
          @instance.after_all
        end
        if @instance.failure && !failed
          failed = true
          _record_minitest_hooks_error(reporter, @instance)
        end
        @instance.name = "around_all" unless failed
      end
    end
  rescue => e
    @instance.capture_exceptions do
      raise e
    end
    _record_minitest_hooks_error(reporter, @instance)
  end
end