2010-03-28 08:15:02 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2011-05-04 05:26:02 -04:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2011-05-04 13:31:43 -04:00
|
|
|
require 'active_support/core_ext/module/remove_method'
|
2010-04-16 21:56:35 -04:00
|
|
|
require 'action_controller'
|
2010-01-31 14:37:43 -05:00
|
|
|
require 'action_controller/test_case'
|
2010-03-07 09:24:30 -05:00
|
|
|
require 'action_view'
|
2010-01-31 14:37:43 -05:00
|
|
|
|
2008-04-19 14:06:57 -04:00
|
|
|
module ActionView
|
2010-06-20 16:26:31 -04:00
|
|
|
# = Action View Test Case
|
2008-04-19 14:06:57 -04:00
|
|
|
class TestCase < ActiveSupport::TestCase
|
2009-09-28 14:31:30 -04:00
|
|
|
class TestController < ActionController::Base
|
2010-03-17 19:28:05 -04:00
|
|
|
include ActionDispatch::TestProcess
|
|
|
|
|
2009-09-28 14:31:30 -04:00
|
|
|
attr_accessor :request, :response, :params
|
|
|
|
|
2010-05-25 01:34:55 -04:00
|
|
|
class << self
|
|
|
|
attr_writer :controller_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def controller_path=(path)
|
|
|
|
self.class.controller_path=(path)
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2010-09-29 05:18:31 -04:00
|
|
|
super
|
2010-05-25 01:34:55 -04:00
|
|
|
self.class.controller_path = ""
|
2009-09-28 14:31:30 -04:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
|
2010-03-02 21:57:02 -05:00
|
|
|
@request.env.delete('PATH_INFO')
|
2009-09-28 14:31:30 -04:00
|
|
|
@params = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
module Behavior
|
|
|
|
extend ActiveSupport::Concern
|
2008-11-07 15:42:34 -05:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
include ActionDispatch::Assertions, ActionDispatch::TestProcess
|
|
|
|
include ActionController::TemplateAssertions
|
|
|
|
include ActionView::Context
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-07-20 15:07:59 -04:00
|
|
|
include ActionDispatch::Routing::PolymorphicRoutes
|
2010-05-04 10:00:46 -04:00
|
|
|
include ActionController::RecordIdentifier
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
include AbstractController::Helpers
|
|
|
|
include ActionView::Helpers
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2011-05-04 05:26:02 -04:00
|
|
|
delegate :lookup_context, :to => :controller
|
2010-05-04 10:00:46 -04:00
|
|
|
attr_accessor :controller, :output_buffer, :rendered
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
module ClassMethods
|
|
|
|
def tests(helper_class)
|
2011-10-03 04:05:25 -04:00
|
|
|
case helper_class
|
|
|
|
when String, Symbol
|
|
|
|
self.helper_class = "#{helper_class.to_s.underscore}_helper".camelize.safe_constantize
|
|
|
|
when Module
|
|
|
|
self.helper_class = helper_class
|
|
|
|
end
|
2010-05-04 10:00:46 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
def determine_default_helper_class(name)
|
2011-09-23 10:46:33 -04:00
|
|
|
mod = name.sub(/Test$/, '').safe_constantize
|
2010-05-04 10:00:46 -04:00
|
|
|
mod.is_a?(Class) ? nil : mod
|
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-04 10:00:46 -04: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)
|
2011-10-03 02:36:13 -04:00
|
|
|
_test_case.send(%(#{method}), *args, &block) # _test_case.send(%(current_user), *args, &block)
|
2010-05-04 10:00:46 -04:00
|
|
|
end # end
|
|
|
|
end_eval
|
|
|
|
end
|
|
|
|
end
|
2010-03-03 20:36:08 -05:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
attr_writer :helper_class
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
def helper_class
|
|
|
|
@helper_class ||= determine_default_helper_class(name)
|
|
|
|
end
|
2008-04-19 14:06:57 -04:00
|
|
|
|
2010-09-13 15:51:42 -04:00
|
|
|
def new(*)
|
|
|
|
include_helper_modules!
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
private
|
2008-04-19 14:06:57 -04:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
def include_helper_modules!
|
|
|
|
helper(helper_class) if helper_class
|
|
|
|
include _helpers
|
2008-04-19 14:06:57 -04:00
|
|
|
end
|
2010-05-04 10:00:46 -04:00
|
|
|
|
2008-04-19 14:06:57 -04:00
|
|
|
end
|
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
def setup_with_controller
|
|
|
|
@controller = ActionView::TestCase::TestController.new
|
2010-06-18 23:20:10 -04:00
|
|
|
@request = @controller.request
|
2010-05-04 10:00:46 -04:00
|
|
|
@output_buffer = ActiveSupport::SafeBuffer.new
|
|
|
|
@rendered = ''
|
|
|
|
|
|
|
|
make_test_case_available_to_view!
|
2010-05-26 00:46:00 -04:00
|
|
|
say_no_to_protect_against_forgery!
|
2008-04-19 14:06:57 -04:00
|
|
|
end
|
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
def config
|
|
|
|
@controller.config if @controller.respond_to?(:config)
|
2008-05-26 04:38:56 -04:00
|
|
|
end
|
2008-06-08 23:05:39 -04:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
def render(options = {}, local_assigns = {}, &block)
|
2010-10-02 13:35:17 -04:00
|
|
|
view.assign(view_assigns)
|
2010-06-23 11:19:13 -04:00
|
|
|
@rendered << output = view.render(options, local_assigns, &block)
|
2010-05-04 10:00:46 -04:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2010-06-22 02:40:22 -04:00
|
|
|
def locals
|
|
|
|
@locals ||= {}
|
|
|
|
end
|
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
included do
|
|
|
|
setup :setup_with_controller
|
|
|
|
end
|
2008-04-19 14:06:57 -04:00
|
|
|
|
2009-09-28 14:31:30 -04:00
|
|
|
private
|
2010-05-04 10:00:46 -04:00
|
|
|
|
|
|
|
# Support the selector assertions
|
|
|
|
#
|
|
|
|
# Need to experiment if this priority is the best one: rendered => output_buffer
|
2011-04-05 10:03:36 -04:00
|
|
|
def response_from_page
|
2010-05-04 10:00:46 -04:00
|
|
|
HTML::Document.new(@rendered.blank? ? @output_buffer : @rendered).root
|
|
|
|
end
|
|
|
|
|
2010-05-26 00:46:00 -04:00
|
|
|
def say_no_to_protect_against_forgery!
|
|
|
|
_helpers.module_eval do
|
2011-05-04 13:31:43 -04:00
|
|
|
remove_possible_method :protect_against_forgery?
|
2010-05-26 00:46:00 -04:00
|
|
|
def protect_against_forgery?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-28 14:31:30 -04:00
|
|
|
def make_test_case_available_to_view!
|
|
|
|
test_case_instance = self
|
|
|
|
_helpers.module_eval do
|
2010-09-28 14:21:44 -04:00
|
|
|
unless private_method_defined?(:_test_case)
|
|
|
|
define_method(:_test_case) { test_case_instance }
|
|
|
|
private :_test_case
|
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
|
|
|
end
|
2008-04-19 14:06:57 -04:00
|
|
|
|
2010-06-22 02:40:22 -04:00
|
|
|
module Locals
|
|
|
|
attr_accessor :locals
|
|
|
|
|
2011-05-01 05:14:38 -04:00
|
|
|
def render(options = {}, local_assigns = {})
|
|
|
|
case options
|
|
|
|
when Hash
|
|
|
|
if block_given?
|
|
|
|
locals[options[:layout]] = options[:locals]
|
|
|
|
elsif options.key?(:partial)
|
|
|
|
locals[options[:partial]] = options[:locals]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
locals[options] = local_assigns
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
2010-06-22 02:40:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-23 11:19:13 -04:00
|
|
|
# The instance of ActionView::Base that is used by +render+.
|
|
|
|
def view
|
|
|
|
@view ||= begin
|
2011-01-12 13:44:56 -05:00
|
|
|
view = @controller.view_context
|
2010-09-29 05:18:31 -04:00
|
|
|
view.singleton_class.send :include, _helpers
|
|
|
|
view.extend(Locals)
|
|
|
|
view.locals = self.locals
|
|
|
|
view.output_buffer = self.output_buffer
|
|
|
|
view
|
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2009-08-10 02:54:17 -04:00
|
|
|
|
2010-06-23 11:19:13 -04:00
|
|
|
alias_method :_view, :view
|
|
|
|
|
2010-10-02 13:35:17 -04:00
|
|
|
INTERNAL_IVARS = %w{
|
|
|
|
@__name__
|
2011-01-17 17:53:44 -05:00
|
|
|
@__io__
|
2010-06-22 10:45:44 -04:00
|
|
|
@_assertion_wrapped
|
2010-10-02 13:35:17 -04:00
|
|
|
@_assertions
|
2010-05-04 10:00:46 -04:00
|
|
|
@_result
|
2010-10-02 13:35:17 -04:00
|
|
|
@_routes
|
2010-06-22 10:45:44 -04:00
|
|
|
@controller
|
|
|
|
@layouts
|
|
|
|
@locals
|
|
|
|
@method_name
|
2009-09-28 14:31:30 -04:00
|
|
|
@output_buffer
|
2010-06-22 10:45:44 -04:00
|
|
|
@partials
|
2010-10-02 13:35:17 -04:00
|
|
|
@passed
|
2010-05-04 10:00:46 -04:00
|
|
|
@rendered
|
2010-06-22 10:45:44 -04:00
|
|
|
@request
|
|
|
|
@routes
|
2010-05-04 10:00:46 -04:00
|
|
|
@templates
|
2011-03-08 17:09:44 -05:00
|
|
|
@options
|
2009-09-28 14:31:30 -04:00
|
|
|
@test_passed
|
2010-06-23 11:19:13 -04:00
|
|
|
@view
|
2010-06-22 10:45:44 -04:00
|
|
|
@view_context_class
|
2009-09-28 14:31:30 -04:00
|
|
|
}
|
|
|
|
|
2010-10-02 13:35:17 -04:00
|
|
|
def _user_defined_ivars
|
|
|
|
instance_variables.map(&:to_s) - INTERNAL_IVARS
|
2008-04-19 14:06:57 -04:00
|
|
|
end
|
|
|
|
|
2010-10-02 13:35:17 -04: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 11:34:34 -04:00
|
|
|
Hash[_user_defined_ivars.map do |var|
|
2010-10-03 18:38:17 -04:00
|
|
|
[var[1, var.length].to_sym, instance_variable_get(var)]
|
2010-10-03 11:34:34 -04:00
|
|
|
end]
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2008-06-08 23:05:39 -04:00
|
|
|
|
2010-07-01 18:29:20 -04:00
|
|
|
def _routes
|
|
|
|
@controller._routes if @controller.respond_to?(:_routes)
|
2010-04-03 05:30:06 -04:00
|
|
|
end
|
|
|
|
|
2008-04-19 14:06:57 -04:00
|
|
|
def method_missing(selector, *args)
|
2010-07-01 18:29:20 -04:00
|
|
|
if @controller.respond_to?(:_routes) &&
|
2010-09-29 05:18:31 -04:00
|
|
|
@controller._routes.named_routes.helpers.include?(selector)
|
2009-09-28 14:31:30 -04:00
|
|
|
@controller.__send__(selector, *args)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2008-04-19 14:06:57 -04:00
|
|
|
end
|
2010-05-04 10:00:46 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
include Behavior
|
|
|
|
|
2008-04-19 14:06:57 -04:00
|
|
|
end
|
|
|
|
end
|