1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
# frozen_string_literal: true
require "active_support/callbacks"
module ActiveSupport
module Testing
# Adds support for +setup+ and +teardown+ callbacks.
# These callbacks serve as a replacement to overwriting the
# <tt>#setup</tt> and <tt>#teardown</tt> methods of your TestCase.
#
# class ExampleTest < ActiveSupport::TestCase
# setup do
# # ...
# end
#
# teardown do
# # ...
# end
# end
module SetupAndTeardown
def self.prepended(klass)
klass.include ActiveSupport::Callbacks
klass.define_callbacks :setup, :teardown
klass.extend ClassMethods
end
module ClassMethods
# Add a callback, which runs before <tt>TestCase#setup</tt>.
def setup(*args, &block)
set_callback(:setup, :before, *args, &block)
end
# Add a callback, which runs after <tt>TestCase#teardown</tt>.
def teardown(*args, &block)
set_callback(:teardown, :after, *args, &block)
end
end
def before_setup # :nodoc:
super
run_callbacks :setup
end
def after_teardown # :nodoc:
begin
run_callbacks :teardown
rescue => e
self.failures << Minitest::UnexpectedError.new(e)
end
super
end
end
end
end
|