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
-
#after(type = nil, &block) ⇒ Object
If type is :all, set the after_all hook instead of the after hook.
-
#around(type = nil, &block) ⇒ Object
If type is :all, set the around_all hook, otherwise set the around hook.
-
#before(type = nil, &block) ⇒ Object
If type is :all, set the before_all hook instead of the before hook.
-
#new(name) ⇒ Object
Unless name is NEW, return a dup singleton instance.
-
#run_suite(reporter, options = {}) ⇒ Object
:nocov:.
-
#with_info_handler(*args, &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.
Instance Method Details
#after(type = nil, &block) ⇒ Object
If type is :all, set the after_all hook instead of the after hook.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/minitest/hooks/test.rb', line 161 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.
142 143 144 145 |
# File 'lib/minitest/hooks/test.rb', line 142 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.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/minitest/hooks/test.rb', line 148 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/minitest/hooks/test.rb', line 50 def new(name) if name.equal?(NEW) instance = super('around_all') # Attempt to handle assertion count issues. Assertions # are incremented on this instance (created once per test class), but # recorded on the instance created once per test method. This forwards # the assertion increment to the most recently created instance. def instance.assertions if @instance @instance.assertions else super end end def instance.assertions=(v) if @instance @instance.assertions = v else super end end return instance end instance = @instance.dup # Reset the assertions for the shared instance after it is # copied into the current instance, so that assertions during # before_all are counted. @instance.instance_variable_set(:@assertions, 0) @instance.instance_variable_set(:@instance, instance) instance.name = name instance.failures = [] instance end |
#run_suite(reporter, options = {}) ⇒ Object
:nocov:
91 92 93 94 |
# File 'lib/minitest/hooks/test.rb', line 91 def run_suite(reporter, = {}) @_minitest_hooks_reporter = reporter super end |
#with_info_handler(*args, &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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/minitest/hooks/test.rb', line 100 def with_info_handler(*args, &block) @instance = new(NEW) @instance.time = 0 @instance.name = "around_all" reporter = args[0] || @_minitest_hooks_reporter # Minitest 6+ 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 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 |