test_case.rb 6.0 KB
Newer Older
1
require 'active_support/core_ext/object/blank'
W
wycats 已提交
2
require 'action_controller'
3
require 'action_controller/test_case'
4
require 'action_view'
5

6
module ActionView
R
Rizwan Reza 已提交
7
  # = Action View Test Case
8
  class TestCase < ActiveSupport::TestCase
9
    class TestController < ActionController::Base
10 11
      include ActionDispatch::TestProcess

12 13
      attr_accessor :request, :response, :params

14 15 16 17 18 19
      class << self
        attr_writer :controller_path
      end

      def controller_path=(path)
        self.class.controller_path=(path)
20 21 22
      end

      def initialize
23
        super
24
        self.class.controller_path = ""
25 26 27
        @request = ActionController::TestRequest.new
        @response = ActionController::TestResponse.new

28
        @request.env.delete('PATH_INFO')
29 30 31 32
        @params = {}
      end
    end

33 34
    module Behavior
      extend ActiveSupport::Concern
35

36 37 38
      include ActionDispatch::Assertions, ActionDispatch::TestProcess
      include ActionController::TemplateAssertions
      include ActionView::Context
39

40
      include ActionDispatch::Routing::PolymorphicRoutes
41
      include ActionController::RecordIdentifier
42

43 44
      include AbstractController::Helpers
      include ActionView::Helpers
45

46
      attr_accessor :controller, :output_buffer, :rendered
47

48 49 50 51
      module ClassMethods
        def tests(helper_class)
          self.helper_class = helper_class
        end
52

53 54 55 56 57 58
        def determine_default_helper_class(name)
          mod = name.sub(/Test$/, '').constantize
          mod.is_a?(Class) ? nil : mod
        rescue NameError
          nil
        end
59

60 61 62 63 64 65 66 67 68 69
        def helper_method(*methods)
          # Almost a duplicate from ActionController::Helpers
          methods.flatten.each do |method|
            _helpers.module_eval <<-end_eval
              def #{method}(*args, &block)                    # def current_user(*args, &block)
                _test_case.send(%(#{method}), *args, &block)  #   test_case.send(%(current_user), *args, &block)
              end                                             # end
            end_eval
          end
        end
70

71
        attr_writer :helper_class
72

73 74 75
        def helper_class
          @helper_class ||= determine_default_helper_class(name)
        end
76

77 78 79 80 81
        def new(*)
          include_helper_modules!
          super
        end

82
      private
83

84 85 86
        def include_helper_modules!
          helper(helper_class) if helper_class
          include _helpers
87
        end
88

89 90
      end

91 92
      def setup_with_controller
        @controller = ActionView::TestCase::TestController.new
93
        @request = @controller.request
94 95 96 97
        @output_buffer = ActiveSupport::SafeBuffer.new
        @rendered = ''

        make_test_case_available_to_view!
98
        say_no_to_protect_against_forgery!
99 100
      end

101 102
      def config
        @controller.config if @controller.respond_to?(:config)
103
      end
104

105
      def render(options = {}, local_assigns = {}, &block)
106
        view.assign(view_assigns)
107
        @rendered << output = view.render(options, local_assigns, &block)
108 109 110
        output
      end

111 112 113 114
      def locals
        @locals ||= {}
      end

115 116 117
      included do
        setup :setup_with_controller
      end
118

119
    private
120 121 122 123 124 125 126 127

      # Support the selector assertions
      #
      # Need to experiment if this priority is the best one: rendered => output_buffer
      def response_from_page_or_rjs
        HTML::Document.new(@rendered.blank? ? @output_buffer : @rendered).root
      end

128 129
      def say_no_to_protect_against_forgery!
        _helpers.module_eval do
130
          remove_method :protect_against_forgery? if method_defined?(:protect_against_forgery?)
131 132 133 134 135 136
          def protect_against_forgery?
            false
          end
        end
      end

137 138 139
      def make_test_case_available_to_view!
        test_case_instance = self
        _helpers.module_eval do
140 141 142 143
          unless private_method_defined?(:_test_case)
            define_method(:_test_case) { test_case_instance }
            private :_test_case
          end
144 145
        end
      end
146

147 148 149 150 151 152 153 154 155
      module Locals
        attr_accessor :locals

        def _render_partial(options)
          locals[options[:partial]] = options[:locals]
          super(options)
        end
      end

156 157 158
      # The instance of ActionView::Base that is used by +render+.
      def view
        @view ||= begin
159
          view = @controller.view_context
160 161 162 163 164 165
          view.singleton_class.send :include, _helpers
          view.extend(Locals)
          view.locals = self.locals
          view.output_buffer = self.output_buffer
          view
        end
166
      end
167

168 169
      alias_method :_view, :view

170 171
      INTERNAL_IVARS = %w{
        @__name__
172
        @__io__
173
        @_assertion_wrapped
174
        @_assertions
175
        @_result
176
        @_routes
177 178 179 180
        @controller
        @layouts
        @locals
        @method_name
181
        @output_buffer
182
        @partials
183
        @passed
184
        @rendered
185 186
        @request
        @routes
187
        @templates
188
        @test_passed
189
        @view
190
        @view_context_class
191 192
      }

193 194
      def _user_defined_ivars
        instance_variables.map(&:to_s) - INTERNAL_IVARS
195 196
      end

197 198 199 200 201
      # Returns a Hash of instance variables and their values, as defined by
      # the user in the test case, which are then assigned to the view being
      # rendered. This is generally intended for internal use and extension
      # frameworks.
      def view_assigns
202
        Hash[_user_defined_ivars.map do |var|
203
          [var[1, var.length].to_sym, instance_variable_get(var)]
204
        end]
205
      end
206

207 208 209 210 211 212
      def _assigns
        ActiveSupport::Deprecation.warn "ActionView::TestCase#_assigns is deprecated and will be removed in future versions. " <<
          "Please use view_assigns instead."
        view_assigns
      end

213 214
      def _routes
        @controller._routes if @controller.respond_to?(:_routes)
215 216
      end

217
      def method_missing(selector, *args)
218
        if @controller.respond_to?(:_routes) &&
219
          @controller._routes.named_routes.helpers.include?(selector)
220 221 222 223
          @controller.__send__(selector, *args)
        else
          super
        end
224
      end
225 226 227 228 229

    end

    include Behavior

230 231
  end
end