aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/test_case.rb
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2008-04-19 13:06:57 -0500
committerJoshua Peek <josh@joshpeek.com>2008-04-19 13:08:24 -0500
commit17d4164a16e5fe7b252375211424a2999a331291 (patch)
tree772695335b861e70caa15d92cd77ca568406cb7f /actionpack/lib/action_view/test_case.rb
parentef4c65088fb907fc819e6b5d83d284c38cdaabfc (diff)
downloadrails-17d4164a16e5fe7b252375211424a2999a331291.tar.gz
rails-17d4164a16e5fe7b252375211424a2999a331291.tar.bz2
rails-17d4164a16e5fe7b252375211424a2999a331291.zip
Introduce ActionView::TestCase for testing view helpers.
Diffstat (limited to 'actionpack/lib/action_view/test_case.rb')
-rw-r--r--actionpack/lib/action_view/test_case.rb64
1 files changed, 64 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
new file mode 100644
index 0000000000..b2e6589d81
--- /dev/null
+++ b/actionpack/lib/action_view/test_case.rb
@@ -0,0 +1,64 @@
+require 'active_support/test_case'
+
+module ActionView
+ class NonInferrableHelperError < ActionViewError
+ def initialize(name)
+ super "Unable to determine the helper to test from #{name}. " +
+ "You'll need to specify it using tests YourHelper in your " +
+ "test case definition"
+ end
+ end
+
+ class TestCase < ActiveSupport::TestCase
+ class_inheritable_accessor :helper_class
+ @@helper_class = nil
+
+ 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
+ raise NonInferrableHelperError.new(name)
+ end
+ end
+
+ ActionView::Base.helper_modules.each do |helper_module|
+ include helper_module
+ end
+ include ActionController::PolymorphicRoutes
+ include ActionController::RecordIdentifier
+
+ setup :setup_with_helper_class
+
+ def setup_with_helper_class
+ self.class.send(:include, helper_class)
+ end
+
+ class TestController < ActionController::Base
+ attr_accessor :request, :response
+
+ def initialize
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+ end
+
+ private
+ def method_missing(selector, *args)
+ controller = TestController.new
+ return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
+ super
+ end
+ end
+end