2010-02-01 03:37:43 +08:00
|
|
|
require 'action_controller/test_case'
|
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
module ActionView
|
2008-12-12 01:06:35 +08:00
|
|
|
class Base
|
|
|
|
alias_method :initialize_without_template_tracking, :initialize
|
|
|
|
def initialize(*args)
|
|
|
|
@_rendered = { :template => nil, :partials => Hash.new(0) }
|
|
|
|
initialize_without_template_tracking(*args)
|
|
|
|
end
|
2009-04-29 12:29:29 +08:00
|
|
|
|
|
|
|
attr_internal :rendered
|
2009-08-10 14:54:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
class Template
|
|
|
|
alias_method :render_without_tracking, :render
|
|
|
|
def render(view, locals, &blk)
|
|
|
|
rendered = view.rendered
|
|
|
|
rendered[:partials][self] += 1 if partial?
|
|
|
|
rendered[:template] ||= []
|
|
|
|
rendered[:template] << self
|
|
|
|
render_without_tracking(view, locals, &blk)
|
2009-04-29 12:29:29 +08:00
|
|
|
end
|
2008-12-12 01:06:35 +08:00
|
|
|
end
|
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
class TestCase < ActiveSupport::TestCase
|
2009-09-29 02:31:30 +08:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
attr_accessor :request, :response, :params
|
|
|
|
|
|
|
|
def self.controller_path
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@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
|
|
|
|
|
2009-12-13 08:09:44 +08:00
|
|
|
include ActionDispatch::Assertions, ActionDispatch::TestProcess
|
2009-07-19 23:58:59 +08:00
|
|
|
include ActionView::Context
|
2008-11-08 04:42:34 +08:00
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
include ActionController::PolymorphicRoutes
|
|
|
|
include ActionController::RecordIdentifier
|
|
|
|
|
|
|
|
include ActionView::Helpers
|
|
|
|
include ActionController::Helpers
|
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
class_inheritable_accessor :helper_class
|
2009-09-29 02:31:30 +08:00
|
|
|
attr_accessor :controller, :output_buffer, :rendered
|
|
|
|
|
|
|
|
setup :setup_with_controller
|
|
|
|
def setup_with_controller
|
|
|
|
@controller = TestController.new
|
For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self).
* Additionally, instead of doing concat("</form>".html_safe), you can do
safe_concat("</form>"), which will skip both the flag set, and the flag
check.
* For the first pass, I converted virtually all #html_safe!s to #html_safe,
and the tests pass. A further optimization would be to try to use
#safe_concat as much as possible, reducing the performance impact if
we know up front that a String is safe.
2010-02-01 11:17:42 +08:00
|
|
|
@output_buffer = ActiveSupport::SafeBuffer.new
|
2009-09-29 02:31:30 +08:00
|
|
|
@rendered = ''
|
|
|
|
|
|
|
|
self.class.send(:include_helper_modules!)
|
|
|
|
make_test_case_available_to_view!
|
|
|
|
end
|
|
|
|
|
2010-03-04 09:36:08 +08:00
|
|
|
def config
|
|
|
|
@controller.config
|
|
|
|
end
|
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
def render(options = {}, local_assigns = {}, &block)
|
|
|
|
@rendered << output = _view.render(options, local_assigns, &block)
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
|
|
|
def protect_against_forgery?
|
|
|
|
false
|
|
|
|
end
|
2008-04-20 02:06:57 +08:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def tests(helper_class)
|
|
|
|
self.helper_class = helper_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def helper_class
|
|
|
|
if current_helper_class = read_inheritable_attribute(:helper_class)
|
|
|
|
current_helper_class
|
|
|
|
else
|
|
|
|
self.helper_class = determine_default_helper_class(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def determine_default_helper_class(name)
|
|
|
|
name.sub(/Test$/, '').constantize
|
|
|
|
rescue NameError
|
2008-05-26 16:38:56 +08:00
|
|
|
nil
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
|
|
|
|
2009-09-29 02:31:30 +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
|
2008-05-26 16:38:56 +08:00
|
|
|
end
|
2008-06-09 11:05:39 +08:00
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
private
|
|
|
|
def include_helper_modules!
|
|
|
|
helper(helper_class) if helper_class
|
|
|
|
include _helpers
|
|
|
|
end
|
2008-04-20 02:06:57 +08:00
|
|
|
end
|
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
private
|
|
|
|
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
|
|
|
|
2009-09-29 02:31:30 +08:00
|
|
|
def _view
|
|
|
|
view = ActionView::Base.new(ActionController::Base.view_paths, _assigns, @controller)
|
|
|
|
view.class.send :include, _helpers
|
2009-11-07 06:02:55 +08:00
|
|
|
view.output_buffer = self.output_buffer
|
2009-09-29 02:31:30 +08:00
|
|
|
view
|
|
|
|
end
|
2009-08-10 14:54:17 +08:00
|
|
|
|
2009-09-29 02:31:30 +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
|
|
|
|
|
|
|
|
EXCLUDE_IVARS = %w{
|
|
|
|
@output_buffer
|
|
|
|
@fixture_cache
|
|
|
|
@method_name
|
|
|
|
@_result
|
|
|
|
@loaded_fixtures
|
|
|
|
@test_passed
|
|
|
|
@view
|
|
|
|
}
|
|
|
|
|
|
|
|
def _instance_variables
|
|
|
|
instance_variables - 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
|
|
|
|
2008-04-20 02:06:57 +08:00
|
|
|
def method_missing(selector, *args)
|
2010-03-03 06:40:59 +08:00
|
|
|
if @controller._router.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
|
|
|
|
end
|
|
|
|
end
|