aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/lib/action_view/test_case.rb
blob: c8f204046bbf3f9366b5901ee54f1d1787bfc1fc (plain) (tree)
1
2
3
4
5
6
7
8
9

                                  
                 





                                                                   





                                                                                 
         

                                                                         

     
                                          
                                                  
                                         
 


















                                                                           
           


         
                               





                                               


                                                                     

                             


                                                 
                                                



                                                      


                                     


         


                                  


                                         
                                                                                                                                



             
require 'active_support/test_case'

module ActionView
  class Base
    alias_method :initialize_without_template_tracking, :initialize
    def initialize(*args)
      @_rendered = { :template => nil, :partials => Hash.new(0) }
      initialize_without_template_tracking(*args)
    end
    
    alias_method :_render_template_without_template_tracking, :_render_template
    def _render_template(template, local_assigns = {})
      if template.respond_to?(:path) && !template.is_a?(InlineTemplate)
        @_rendered[:partials][template] += 1 if template.is_a?(RenderablePartial)
        @_rendered[:template] ||= template
      end
      _render_template_without_template_tracking(template, local_assigns)
    end    
  end

  class TestCase < ActiveSupport::TestCase
    include ActionController::TestCase::Assertions
    include ActionController::TestProcess

    class_inheritable_accessor :helper_class
    @@helper_class = nil

    class << self
      def tests(helper_class)
        self.helper_class = helper_class
      end

      def helper_class
        if current_helper_class = read_inheritable_attribute(:helper_class)
          current_helper_class
        else
          self.helper_class = determine_default_helper_class(name)
        end
      end

      def determine_default_helper_class(name)
        name.sub(/Test$/, '').constantize
      rescue NameError
        nil
      end
    end

    include ActionView::Helpers
    include ActionController::PolymorphicRoutes
    include ActionController::RecordIdentifier

    setup :setup_with_helper_class

    def setup_with_helper_class
      if helper_class && !self.class.ancestors.include?(helper_class)
        self.class.send(:include, helper_class)
      end

      self.output_buffer = ''
    end

    class TestController < ActionController::Base
      attr_accessor :request, :response, :params

      def initialize
        @request = ActionController::TestRequest.new
        @response = ActionController::TestResponse.new
        
        @params = {}
        send(:initialize_current_url)
      end
    end

    protected
      attr_accessor :output_buffer

    private
      def method_missing(selector, *args)
        controller = TestController.new
        return controller.__send__(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
        super
      end
  end
end