2010-03-28 20:15:02 +08:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2010-04-17 09:56:35 +08:00
|
|
|
require 'action_controller'
|
2010-02-01 03:37:43 +08:00
|
|
|
require 'action_controller/test_case'
|
2010-03-07 22:24:30 +08:00
|
|
|
require 'action_view'
|
2010-02-01 03:37:43 +08:00
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
module ActionView
|
2010-06-21 04:26:31 +08:00
|
|
|
# = Action View Test Case
|
2008-04-20 02:06:57 +08:00
|
|
|
class TestCase < ActiveSupport::TestCase
|
2009-09-29 02:31:30 +08:00
|
|
|
class TestController < ActionController::Base
|
2010-03-18 07:28:05 +08:00
|
|
|
include ActionDispatch::TestProcess
|
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
attr_accessor :request, :response, :params
|
|
|
|
|
2010-05-25 13:34:55 +08:00
|
|
|
class << self
|
|
|
|
attr_writer :controller_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def controller_path=(path)
|
|
|
|
self.class.controller_path=(path)
|
2009-09-29 02:31:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2010-05-25 13:34:55 +08:00
|
|
|
self.class.controller_path = ""
|
2009-09-29 02:31:30 +08:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
|
2010-03-03 10:57:02 +08:00
|
|
|
@request.env.delete('PATH_INFO')
|
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
@params = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
module Behavior
|
|
|
|
extend ActiveSupport::Concern
|
2008-11-08 04:42:34 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
include ActionDispatch::Assertions, ActionDispatch::TestProcess
|
|
|
|
include ActionController::TemplateAssertions
|
|
|
|
include ActionView::Context
|
2009-09-29 02:31:30 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
include ActionController::PolymorphicRoutes
|
|
|
|
include ActionController::RecordIdentifier
|
2009-09-29 02:31:30 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
include AbstractController::Helpers
|
|
|
|
include ActionView::Helpers
|
2009-09-29 02:31:30 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
attr_accessor :controller, :output_buffer, :rendered
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def tests(helper_class)
|
|
|
|
self.helper_class = helper_class
|
|
|
|
end
|
2009-09-29 02:31:30 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
def determine_default_helper_class(name)
|
|
|
|
mod = name.sub(/Test$/, '').constantize
|
|
|
|
mod.is_a?(Class) ? nil : mod
|
|
|
|
rescue NameError
|
|
|
|
nil
|
|
|
|
end
|
2009-09-29 02:31:30 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
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
|
2010-03-04 09:36:08 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
attr_writer :helper_class
|
2009-09-29 02:31:30 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
def helper_class
|
|
|
|
@helper_class ||= determine_default_helper_class(name)
|
|
|
|
end
|
2008-04-20 02:06:57 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
private
|
2008-04-20 02:06:57 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
def include_helper_modules!
|
|
|
|
helper(helper_class) if helper_class
|
|
|
|
include _helpers
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
2010-05-04 22:00:46 +08:00
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
def setup_with_controller
|
|
|
|
@controller = ActionView::TestCase::TestController.new
|
2010-06-19 11:20:10 +08:00
|
|
|
@request = @controller.request
|
2010-05-04 22:00:46 +08:00
|
|
|
@output_buffer = ActiveSupport::SafeBuffer.new
|
|
|
|
@rendered = ''
|
|
|
|
|
|
|
|
self.class.send(:include_helper_modules!)
|
|
|
|
make_test_case_available_to_view!
|
2010-05-26 12:46:00 +08:00
|
|
|
say_no_to_protect_against_forgery!
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
def config
|
|
|
|
@controller.config if @controller.respond_to?(:config)
|
2008-05-26 16:38:56 +08:00
|
|
|
end
|
2008-06-09 11:05:39 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
def render(options = {}, local_assigns = {}, &block)
|
2010-06-23 23:19:13 +08:00
|
|
|
view.assign(_assigns)
|
|
|
|
@rendered << output = view.render(options, local_assigns, &block)
|
2010-05-04 22:00:46 +08:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2010-06-22 14:40:22 +08:00
|
|
|
def locals
|
|
|
|
@locals ||= {}
|
|
|
|
end
|
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
included do
|
|
|
|
setup :setup_with_controller
|
|
|
|
end
|
2008-04-20 02:06:57 +08:00
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
private
|
2010-05-04 22:00:46 +08:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2010-05-26 12:46:00 +08:00
|
|
|
def say_no_to_protect_against_forgery!
|
|
|
|
_helpers.module_eval do
|
|
|
|
def protect_against_forgery?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
def make_test_case_available_to_view!
|
|
|
|
test_case_instance = self
|
|
|
|
_helpers.module_eval do
|
|
|
|
define_method(:_test_case) { test_case_instance }
|
|
|
|
private :_test_case
|
|
|
|
end
|
|
|
|
end
|
2008-04-20 02:06:57 +08:00
|
|
|
|
2010-06-22 14:40:22 +08:00
|
|
|
module Locals
|
|
|
|
attr_accessor :locals
|
|
|
|
|
|
|
|
def _render_partial(options)
|
|
|
|
locals[options[:partial]] = options[:locals]
|
|
|
|
super(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-23 23:19:13 +08:00
|
|
|
# The instance of ActionView::Base that is used by +render+.
|
|
|
|
def view
|
|
|
|
@view ||= begin
|
2010-06-23 20:59:56 +08:00
|
|
|
view = ActionView::Base.new(ActionController::Base.view_paths, {}, @controller)
|
2010-06-09 03:18:02 +08:00
|
|
|
view.singleton_class.send :include, _helpers
|
2010-07-02 06:29:20 +08:00
|
|
|
view.singleton_class.send :include, @controller._routes.url_helpers
|
2010-06-09 03:18:02 +08:00
|
|
|
view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"
|
2010-06-22 14:40:22 +08:00
|
|
|
view.extend(Locals)
|
|
|
|
view.locals = self.locals
|
2010-06-09 03:18:02 +08:00
|
|
|
view.output_buffer = self.output_buffer
|
|
|
|
view
|
|
|
|
end
|
2009-09-29 02:31:30 +08:00
|
|
|
end
|
2009-08-10 14:54:17 +08:00
|
|
|
|
2010-06-23 23:19:13 +08:00
|
|
|
alias_method :_view, :view
|
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
EXCLUDE_IVARS = %w{
|
2010-06-22 22:45:44 +08:00
|
|
|
@_assertion_wrapped
|
2010-05-04 22:00:46 +08:00
|
|
|
@_result
|
2010-06-22 22:45:44 +08:00
|
|
|
@controller
|
|
|
|
@layouts
|
|
|
|
@locals
|
|
|
|
@method_name
|
2009-09-29 02:31:30 +08:00
|
|
|
@output_buffer
|
2010-06-22 22:45:44 +08:00
|
|
|
@partials
|
2010-05-04 22:00:46 +08:00
|
|
|
@rendered
|
2010-06-22 22:45:44 +08:00
|
|
|
@request
|
|
|
|
@routes
|
2010-05-04 22:00:46 +08:00
|
|
|
@templates
|
2009-09-29 02:31:30 +08:00
|
|
|
@test_passed
|
2010-06-23 23:19:13 +08:00
|
|
|
@view
|
2010-06-22 22:45:44 +08:00
|
|
|
@view_context_class
|
2009-09-29 02:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def _instance_variables
|
2010-06-28 19:11:01 +08:00
|
|
|
instance_variables.map(&:to_s) - EXCLUDE_IVARS
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
def _assigns
|
|
|
|
_instance_variables.inject({}) do |hash, var|
|
|
|
|
name = var[1..-1].to_sym
|
|
|
|
hash[name] = instance_variable_get(var)
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
2008-06-09 11:05:39 +08:00
|
|
|
|
2010-07-02 06:29:20 +08:00
|
|
|
def _routes
|
|
|
|
@controller._routes if @controller.respond_to?(:_routes)
|
2010-04-03 17:30:06 +08:00
|
|
|
end
|
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
def method_missing(selector, *args)
|
2010-07-02 06:29:20 +08:00
|
|
|
if @controller.respond_to?(:_routes) &&
|
|
|
|
@controller._routes.named_routes.helpers.include?(selector)
|
2009-09-29 02:31:30 +08:00
|
|
|
@controller.__send__(selector, *args)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
2010-05-04 22:00:46 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
include Behavior
|
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
|
|
|
end
|