aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/test_case.rb
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-05-27 10:57:05 +0200
committerXavier Noria <fxn@hashref.com>2010-05-27 10:57:05 +0200
commitddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3 (patch)
treec58ac9162487bdca68342fb2d30730ccfd926378 /actionpack/lib/action_view/test_case.rb
parent96e2094b8b634e4af0d9d3c8a1db9bbb7023a4a7 (diff)
parente02db06ece7aeecec7c37f5b0e3de7d65c8684e6 (diff)
downloadrails-ddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3.tar.gz
rails-ddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3.tar.bz2
rails-ddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3.zip
Merge remote branch 'rails/master'
Diffstat (limited to 'actionpack/lib/action_view/test_case.rb')
-rw-r--r--actionpack/lib/action_view/test_case.rb164
1 files changed, 97 insertions, 67 deletions
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index f6417d5c2c..4dbbd2eb6a 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -10,11 +10,16 @@ module ActionView
attr_accessor :request, :response, :params
- def self.controller_path
- ''
+ class << self
+ attr_writer :controller_path
+ end
+
+ def controller_path=(path)
+ self.class.controller_path=(path)
end
def initialize
+ self.class.controller_path = ""
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@@ -24,80 +29,99 @@ 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 = ''
+ attr_accessor :controller, :output_buffer, :rendered
+
+ module ClassMethods
+ def tests(helper_class)
+ self.helper_class = helper_class
+ end
- self.class.send(:include_helper_modules!)
- make_test_case_available_to_view!
- end
+ def determine_default_helper_class(name)
+ mod = name.sub(/Test$/, '').constantize
+ mod.is_a?(Class) ? nil : mod
+ rescue NameError
+ nil
+ end
- def config
- @controller.config if @controller.respond_to?(:config)
- 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
- def render(options = {}, local_assigns = {}, &block)
- @rendered << output = _view.render(options, local_assigns, &block)
- output
- end
+ attr_writer :helper_class
- def protect_against_forgery?
- false
- end
+ def helper_class
+ @helper_class ||= determine_default_helper_class(name)
+ end
- class << self
- def tests(helper_class)
- self.helper_class = helper_class
- end
+ private
- def helper_class
- if current_helper_class = read_inheritable_attribute(:helper_class)
- current_helper_class
- else
- self.helper_class = determine_default_helper_class(name)
+ def include_helper_modules!
+ helper(helper_class) if helper_class
+ include _helpers
end
+
end
- def determine_default_helper_class(name)
- name.sub(/Test$/, '').constantize
- rescue NameError
- nil
+ 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!
+ say_no_to_protect_against_forgery!
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
+ def config
+ @controller.config if @controller.respond_to?(:config)
end
- private
- def include_helper_modules!
- helper(helper_class) if helper_class
- include _helpers
- end
- 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 say_no_to_protect_against_forgery!
+ _helpers.module_eval do
+ def protect_against_forgery?
+ false
+ end
+ end
+ end
+
def make_test_case_available_to_view!
test_case_instance = self
_helpers.module_eval do
@@ -110,29 +134,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{
+ @_result
@output_buffer
- @fixture_cache
+ @rendered
+ @templates
+ @view_context_class
+ @layouts
+ @partials
+ @controller
+
@method_name
- @_result
+ @fixture_cache
@loaded_fixtures
@test_passed
- @view
}
def _instance_variables
instance_variables - EXCLUDE_IVARS
+ instance_variables
end
def _assigns
@@ -155,5 +180,10 @@ module ActionView
super
end
end
+
+ end
+
+ include Behavior
+
end
end