aboutsummaryrefslogblamecommitdiffstats
path: root/activesupport/lib/active_support/testing/setup_and_teardown.rb
blob: 40b781485ea5b4159bea0849efe6fbe8d1b7e2cd (plain) (tree)
1
2
3
4
5
6
7
8


                                  


                           
                                   
 



                                          
                           








                                                        


           


                        
               
                                   

                            
                               
                                                            

                 
                                     
                                 
                                                              
               
             
                

           
 


       
require 'active_support/concern'
require 'active_support/callbacks'

module ActiveSupport
  module Testing
    module SetupAndTeardown
      extend ActiveSupport::Concern

      included do
        include ActiveSupport::Callbacks
        define_callbacks :setup, :teardown

        include ForMiniTest
      end

      module ClassMethods
        def setup(*args, &block)
          set_callback(:setup, :before, *args, &block)
        end

        def teardown(*args, &block)
          set_callback(:teardown, :after, *args, &block)
        end
      end

      module ForMiniTest
        def run(runner)
          result = '.'
          begin
            run_callbacks :setup do
              result = super
            end
          rescue Exception => e
            result = runner.puke(self.class, method_name, e)
          ensure
            begin
              run_callbacks :teardown
            rescue Exception => e
              result = runner.puke(self.class, method_name, e)
            end
          end
          result
        end
      end

    end
  end
end