2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-25 23:42:21 -04:00
|
|
|
require "active_support/core_ext/module/redefine_method"
|
2016-08-06 12:48:35 -04:00
|
|
|
require "action_controller"
|
|
|
|
require "action_controller/test_case"
|
|
|
|
require "action_view"
|
2010-01-31 14:37:43 -05:00
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
require "rails-dom-testing"
|
2013-10-11 07:02:59 -04: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
|
2016-05-20 22:28:00 -04:00
|
|
|
attr_writer :controller_path
|
2010-05-25 01:34:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def controller_path=(path)
|
2016-10-28 23:05:58 -04:00
|
|
|
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 = ""
|
2016-08-11 01:01:32 -04:00
|
|
|
@request = ActionController::TestRequest.create(self.class)
|
2015-07-14 09:27:42 -04:00
|
|
|
@response = ActionDispatch::TestResponse.new
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
@request.env.delete("PATH_INFO")
|
2015-12-29 10:37:07 -05:00
|
|
|
@params = ActionController::Parameters.new
|
2009-09-28 14:31:30 -04:00
|
|
|
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
|
2013-10-11 07:02:59 -04:00
|
|
|
include Rails::Dom::Testing::Assertions
|
2010-05-04 10:00:46 -04:00
|
|
|
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
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
include AbstractController::Helpers
|
|
|
|
include ActionView::Helpers
|
2012-06-09 08:20:31 -04:00
|
|
|
include ActionView::RecordIdentifier
|
2012-06-21 12:39:35 -04:00
|
|
|
include ActionView::RoutingUrlFor
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2012-09-25 17:39:29 -04:00
|
|
|
include ActiveSupport::Testing::ConstantLookup
|
|
|
|
|
2016-08-06 13:36:34 -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)
|
2012-09-24 17:37:45 -04:00
|
|
|
determine_constant_from_test_name(name) do |constant|
|
|
|
|
Module === constant && !(Class === constant)
|
|
|
|
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 helper_method(*methods)
|
|
|
|
# Almost a duplicate from ActionController::Helpers
|
|
|
|
methods.flatten.each do |method|
|
2017-05-23 02:08:59 -04:00
|
|
|
_helpers.module_eval <<-end_eval, __FILE__, __LINE__ + 1
|
2010-05-04 10:00:46 -04:00
|
|
|
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
|
|
|
|
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
|
2017-07-25 22:27:11 -04:00
|
|
|
@view_flow = ActionView::OutputFlow.new
|
2014-05-27 16:22:06 -04:00
|
|
|
# empty string ensures buffer has UTF-8 encoding as
|
|
|
|
# new without arguments returns ASCII-8BIT encoded buffer like String#new
|
2016-08-06 12:48:35 -04:00
|
|
|
@output_buffer = ActiveSupport::SafeBuffer.new ""
|
2017-06-20 08:20:04 -04:00
|
|
|
@rendered = "".dup
|
2010-05-04 10:00:46 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-10-09 15:14:11 -04:00
|
|
|
def rendered_views
|
|
|
|
@_rendered_views ||= RenderedViewsCollection.new
|
|
|
|
end
|
|
|
|
|
2017-02-20 15:22:42 -05:00
|
|
|
def _routes
|
|
|
|
@controller._routes if @controller.respond_to?(:_routes)
|
|
|
|
end
|
|
|
|
|
2014-11-17 13:37:17 -05:00
|
|
|
# Need to experiment if this priority is the best one: rendered => output_buffer
|
2012-10-09 15:14:11 -04:00
|
|
|
class RenderedViewsCollection
|
|
|
|
def initialize
|
2012-12-15 14:21:40 -05:00
|
|
|
@rendered_views ||= Hash.new { |hash, key| hash[key] = [] }
|
2012-10-09 15:14:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add(view, locals)
|
|
|
|
@rendered_views[view] ||= []
|
|
|
|
@rendered_views[view] << locals
|
|
|
|
end
|
|
|
|
|
|
|
|
def locals_for(view)
|
|
|
|
@rendered_views[view]
|
|
|
|
end
|
|
|
|
|
2012-12-15 14:20:34 -05:00
|
|
|
def rendered_views
|
|
|
|
@rendered_views.keys
|
|
|
|
end
|
|
|
|
|
2012-10-09 15:14:11 -04:00
|
|
|
def view_rendered?(view, expected_locals)
|
|
|
|
locals_for(view).any? do |actual_locals|
|
2016-08-16 03:30:11 -04:00
|
|
|
expected_locals.all? { |key, value| value == actual_locals[key] }
|
2012-10-09 15:14:11 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-22 02:40:22 -04:00
|
|
|
end
|
|
|
|
|
2010-05-04 10:00:46 -04:00
|
|
|
included do
|
|
|
|
setup :setup_with_controller
|
2016-08-25 03:22:48 -04:00
|
|
|
ActiveSupport.run_load_hooks(:action_view_test_case, self)
|
2010-05-04 10:00:46 -04:00
|
|
|
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
|
|
|
|
2014-11-18 15:47:22 -05:00
|
|
|
# Need to experiment if this priority is the best one: rendered => output_buffer
|
|
|
|
def document_root_element
|
|
|
|
Nokogiri::HTML::Document.parse(@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
|
2017-05-25 23:42:21 -04:00
|
|
|
silence_redefinition_of_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
|
2012-10-09 15:14:11 -04:00
|
|
|
attr_accessor :rendered_views
|
2010-06-22 02:40:22 -04:00
|
|
|
|
2011-05-01 05:14:38 -04:00
|
|
|
def render(options = {}, local_assigns = {})
|
|
|
|
case options
|
|
|
|
when Hash
|
|
|
|
if block_given?
|
2012-10-09 15:14:11 -04:00
|
|
|
rendered_views.add options[:layout], options[:locals]
|
2011-05-01 05:14:38 -04:00
|
|
|
elsif options.key?(:partial)
|
2012-10-09 15:14:11 -04:00
|
|
|
rendered_views.add options[:partial], options[:locals]
|
2011-05-01 05:14:38 -04:00
|
|
|
end
|
|
|
|
else
|
2012-10-09 15:14:11 -04:00
|
|
|
rendered_views.add options, local_assigns
|
2011-05-01 05:14:38 -04:00
|
|
|
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
|
2015-01-31 23:12:37 -05:00
|
|
|
view.singleton_class.include(_helpers)
|
2010-09-29 05:18:31 -04:00
|
|
|
view.extend(Locals)
|
2017-01-05 03:20:57 -05:00
|
|
|
view.rendered_views = rendered_views
|
|
|
|
view.output_buffer = output_buffer
|
2010-09-29 05:18:31 -04:00
|
|
|
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
|
|
|
|
|
2012-01-07 07:30:05 -05:00
|
|
|
INTERNAL_IVARS = [
|
2013-05-06 20:38:45 -04:00
|
|
|
:@NAME,
|
|
|
|
:@failures,
|
|
|
|
:@assertions,
|
2012-01-07 07:30:05 -05:00
|
|
|
:@__io__,
|
|
|
|
:@_assertion_wrapped,
|
|
|
|
:@_assertions,
|
|
|
|
:@_result,
|
|
|
|
:@_routes,
|
|
|
|
:@controller,
|
2012-09-30 04:54:50 -04:00
|
|
|
:@_layouts,
|
2013-02-27 10:44:25 -05:00
|
|
|
:@_files,
|
2012-10-09 15:14:11 -04:00
|
|
|
:@_rendered_views,
|
2012-01-07 07:30:05 -05:00
|
|
|
:@method_name,
|
|
|
|
:@output_buffer,
|
2012-09-30 04:54:50 -04:00
|
|
|
:@_partials,
|
2012-01-07 07:30:05 -05:00
|
|
|
:@passed,
|
|
|
|
:@rendered,
|
|
|
|
:@request,
|
|
|
|
:@routes,
|
2012-09-27 17:45:48 -04:00
|
|
|
:@tagged_logger,
|
2012-09-30 04:54:50 -04:00
|
|
|
:@_templates,
|
2012-01-07 07:30:05 -05:00
|
|
|
:@options,
|
|
|
|
:@test_passed,
|
|
|
|
:@view,
|
2014-04-08 00:42:27 -04:00
|
|
|
:@view_context_class,
|
2017-07-25 22:27:11 -04:00
|
|
|
:@view_flow,
|
2013-10-11 17:29:08 -04:00
|
|
|
:@_subscribers,
|
|
|
|
:@html_document
|
2012-01-07 07:30:05 -05:00
|
|
|
]
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-10-02 13:35:17 -04:00
|
|
|
def _user_defined_ivars
|
2012-01-07 07:30:05 -05:00
|
|
|
instance_variables - 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
|
2012-01-07 07:30:05 -05:00
|
|
|
Hash[_user_defined_ivars.map do |ivar|
|
|
|
|
[ivar[1..-1].to_sym, instance_variable_get(ivar)]
|
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
|
|
|
|
2008-04-19 14:06:57 -04:00
|
|
|
def method_missing(selector, *args)
|
2015-08-10 14:01:18 -04:00
|
|
|
begin
|
|
|
|
routes = @controller.respond_to?(:_routes) && @controller._routes
|
|
|
|
rescue
|
|
|
|
# Dont call routes, if there is an error on _routes call
|
|
|
|
end
|
|
|
|
|
|
|
|
if routes &&
|
2016-10-28 23:05:58 -04:00
|
|
|
(routes.named_routes.route_defined?(selector) ||
|
|
|
|
routes.mounted_helpers.method_defined?(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
|
2017-08-03 16:22:58 -04:00
|
|
|
|
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
begin
|
|
|
|
routes = @controller.respond_to?(:_routes) && @controller._routes
|
|
|
|
rescue
|
|
|
|
# Dont call routes, if there is an error on _routes call
|
|
|
|
end
|
|
|
|
|
|
|
|
routes &&
|
|
|
|
(routes.named_routes.route_defined?(name) ||
|
|
|
|
routes.mounted_helpers.method_defined?(name))
|
|
|
|
end
|
2010-05-04 10:00:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
include Behavior
|
2008-04-19 14:06:57 -04:00
|
|
|
end
|
|
|
|
end
|