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-09-29 17:18:31 +08:00
|
|
|
super
|
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-07-21 03:07:59 +08:00
|
|
|
include ActionDispatch::Routing::PolymorphicRoutes
|
2010-05-04 22:00:46 +08:00
|
|
|
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
|
2010-08-14 13:13:00 +08:00
|
|
|
|
2010-05-04 22:00:46 +08:00
|
|
|
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-09-14 03:51:42 +08:00
|
|
|
def new(*)
|
|
|
|
include_helper_modules!
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
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 = ''
|
|
|
|
|
|
|
|
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-10-03 01:35:17 +08:00
|
|
|
view.assign(view_assigns)
|
2010-06-23 23:19:13 +08:00
|
|
|
@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
|
2010-09-25 03:08:04 +08:00
|
|
|
remove_method :protect_against_forgery? if method_defined?(:protect_against_forgery?)
|
2010-05-26 12:46:00 +08:00
|
|
|
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
|
2010-09-29 02:21:44 +08:00
|
|
|
unless private_method_defined?(:_test_case)
|
|
|
|
define_method(:_test_case) { test_case_instance }
|
|
|
|
private :_test_case
|
|
|
|
end
|
2009-09-29 02:31:30 +08:00
|
|
|
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-09-29 17:18:31 +08:00
|
|
|
view = ActionView::Base.new(ActionController::Base.view_paths, {}, @controller)
|
|
|
|
view.singleton_class.send :include, _helpers
|
|
|
|
view.singleton_class.send :include, @controller._routes.url_helpers
|
|
|
|
view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"
|
|
|
|
view.extend(Locals)
|
|
|
|
view.locals = self.locals
|
|
|
|
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
|
|
|
|
|
2010-10-03 01:35:17 +08:00
|
|
|
INTERNAL_IVARS = %w{
|
|
|
|
@__name__
|
2010-06-22 22:45:44 +08:00
|
|
|
@_assertion_wrapped
|
2010-10-03 01:35:17 +08:00
|
|
|
@_assertions
|
2010-05-04 22:00:46 +08:00
|
|
|
@_result
|
2010-10-03 01:35:17 +08:00
|
|
|
@_routes
|
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-10-03 01:35:17 +08:00
|
|
|
@passed
|
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
|
|
|
}
|
|
|
|
|
2010-10-03 01:35:17 +08:00
|
|
|
def _user_defined_ivars
|
|
|
|
instance_variables.map(&:to_s) - INTERNAL_IVARS
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
|
|
|
|
2010-10-03 01:35:17 +08:00
|
|
|
# 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
|
2010-10-03 23:34:34 +08:00
|
|
|
Hash[_user_defined_ivars.map do |var|
|
2010-10-04 06:38:17 +08:00
|
|
|
[var[1, var.length].to_sym, instance_variable_get(var)]
|
2010-10-03 23:34:34 +08:00
|
|
|
end]
|
2009-09-29 02:31:30 +08:00
|
|
|
end
|
2008-06-09 11:05:39 +08:00
|
|
|
|
2010-10-03 01:35:17 +08:00
|
|
|
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
|
|
|
|
|
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) &&
|
2010-09-29 17:18:31 +08:00
|
|
|
@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
|