Sha256: 911d4e8d52d476b1a675ceac22b4c619dc9b92ea790f4d0f51dc30b4e1808943

Contents?: true

Size: 1.4 KB

Versions: 28

Compression:

Stored size: 1.4 KB

Contents

# 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)
        rescue Minitest::Assertion => e
          self.failures << e
        end

        super
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
activesupport-8.0.1 lib/active_support/testing/setup_and_teardown.rb
activesupport-8.0.0.1 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.2.2.1 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.1.5.1 lib/active_support/testing/setup_and_teardown.rb
activesupport-8.0.0 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.2.2 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.1.5 lib/active_support/testing/setup_and_teardown.rb
activesupport-8.0.0.rc2 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.2.1.2 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.1.4.2 lib/active_support/testing/setup_and_teardown.rb
activesupport-8.0.0.rc1 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.2.1.1 lib/active_support/testing/setup_and_teardown.rb
activesupport-7.1.4.1 lib/active_support/testing/setup_and_teardown.rb
activesupport-8.0.0.beta1 lib/active_support/testing/setup_and_teardown.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/testing/setup_and_teardown.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/testing/setup_and_teardown.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/testing/setup_and_teardown.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/testing/setup_and_teardown.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/testing/setup_and_teardown.rb
omg-activesupport-8.0.0.alpha2 lib/active_support/testing/setup_and_teardown.rb