1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move AV::TC to AV::TC::Behavior [#4678 state:resolved]

- enables alternative testing frameworks to include AV::TC::Behavior
  instead of subclassing AV::TC
- also added tests and code for:
  - test view delegates :notice to request.flash
    - useful since generators generate views that use notice
  - test case doesn't try to include modules that are actually
    classes

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
David Chelimsky 2010-05-04 09:00:46 -05:00 committed by José Valim
parent 05c95b5c58
commit 8e583b69e8
2 changed files with 104 additions and 70 deletions

View file

@ -24,80 +24,94 @@ module ActionView
end
end
include ActionDispatch::Assertions, ActionDispatch::TestProcess
include ActionController::TemplateAssertions
include ActionView::Context
module Behavior
extend ActiveSupport::Concern
include ActionController::PolymorphicRoutes
include ActionController::RecordIdentifier
include ActionDispatch::Assertions, ActionDispatch::TestProcess
include ActionController::TemplateAssertions
include ActionView::Context
include AbstractController::Helpers
include ActionView::Helpers
include ActionController::PolymorphicRoutes
include ActionController::RecordIdentifier
class_inheritable_accessor :helper_class
attr_accessor :controller, :output_buffer, :rendered
include AbstractController::Helpers
include ActionView::Helpers
setup :setup_with_controller
def setup_with_controller
@controller = TestController.new
@output_buffer = ActiveSupport::SafeBuffer.new
@rendered = ''
self.class.send(:include_helper_modules!)
make_test_case_available_to_view!
end
def config
@controller.config if @controller.respond_to?(:config)
end
def render(options = {}, local_assigns = {}, &block)
@rendered << output = _view.render(options, local_assigns, &block)
output
end
def protect_against_forgery?
false
end
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)
attr_accessor :controller, :output_buffer, :rendered
module ClassMethods
def tests(helper_class)
self.helper_class = helper_class
end
end
def determine_default_helper_class(name)
name.sub(/Test$/, '').constantize
rescue NameError
nil
end
def helper_method(*methods)
# Almost a duplicate from ActionController::Helpers
methods.flatten.each do |method|
_helpers.module_eval <<-end_eval, __FILE__, __LINE__ + 1
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
def determine_default_helper_class(name)
mod = name.sub(/Test$/, '').constantize
mod.is_a?(Class) ? nil : mod
rescue NameError
nil
end
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
attr_writer :helper_class
def helper_class
@helper_class ||= determine_default_helper_class(name)
end
end
private
def include_helper_modules!
helper(helper_class) if helper_class
include _helpers
end
end
end
def setup_with_controller
@controller = ActionView::TestCase::TestController.new
@output_buffer = ActiveSupport::SafeBuffer.new
@rendered = ''
self.class.send(:include_helper_modules!)
make_test_case_available_to_view!
end
def config
@controller.config if @controller.respond_to?(:config)
end
def protect_against_forgery?
false
end
def render(options = {}, local_assigns = {}, &block)
@rendered << output = _view.render(options, local_assigns, &block)
output
end
included do
setup :setup_with_controller
end
private
# 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
def make_test_case_available_to_view!
test_case_instance = self
_helpers.module_eval do
@ -110,29 +124,30 @@ module ActionView
view = ActionView::Base.new(ActionController::Base.view_paths, _assigns, @controller)
view.singleton_class.send :include, _helpers
view.singleton_class.send :include, @controller._router.url_helpers
view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"
view.output_buffer = self.output_buffer
view
end
# 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
@output_buffer
@rendered
@templates
@view_context_class
@layouts
@partials
@controller
@method_name
@fixture_cache
@loaded_fixtures
@test_passed
@view
}
def _instance_variables
instance_variables - EXCLUDE_IVARS
instance_variables
end
def _assigns
@ -155,5 +170,10 @@ module ActionView
super
end
end
end
include Behavior
end
end

View file

@ -48,6 +48,20 @@ module ActionView
assert test_case.ancestors.include?(AnotherTestHelper)
assert_equal 'Howdy!', from_another_helper
end
test "determine_default_helper_class returns nil if name.sub(/Test$/, '').constantize resolves to a class" do
assert_nil self.class.determine_default_helper_class("String")
end
test "delegates notice to request.flash" do
_view.request.flash.expects(:notice).with("this message")
_view.notice("this message")
end
test "delegates alert to request.flash" do
_view.request.flash.expects(:alert).with("this message")
_view.alert("this message")
end
end
class ClassMethodsTest < ActionView::TestCase