| | 1 | require 'active_support/test_case' |
|---|
| | 2 | |
|---|
| | 3 | module ActionView |
|---|
| | 4 | class NonInferrableHelperError < ActionViewError |
|---|
| | 5 | def initialize(name) |
|---|
| | 6 | super "Unable to determine the helper to test from #{name}. " + |
|---|
| | 7 | "You'll need to specify it using tests YourHelper in your " + |
|---|
| | 8 | "test case definition" |
|---|
| | 9 | end |
|---|
| | 10 | end |
|---|
| | 11 | |
|---|
| | 12 | class TestCase < ActiveSupport::TestCase |
|---|
| | 13 | class_inheritable_accessor :helper_class |
|---|
| | 14 | @@helper_class = nil |
|---|
| | 15 | |
|---|
| | 16 | class << self |
|---|
| | 17 | def tests(helper_class) |
|---|
| | 18 | self.helper_class = helper_class |
|---|
| | 19 | end |
|---|
| | 20 | |
|---|
| | 21 | def helper_class |
|---|
| | 22 | if current_helper_class = read_inheritable_attribute(:helper_class) |
|---|
| | 23 | current_helper_class |
|---|
| | 24 | else |
|---|
| | 25 | self.helper_class = determine_default_helper_class(name) |
|---|
| | 26 | end |
|---|
| | 27 | end |
|---|
| | 28 | |
|---|
| | 29 | def determine_default_helper_class(name) |
|---|
| | 30 | name.sub(/Test$/, '').constantize |
|---|
| | 31 | rescue NameError |
|---|
| | 32 | raise NonInferrableHelperError.new(name) |
|---|
| | 33 | end |
|---|
| | 34 | end |
|---|
| | 35 | |
|---|
| | 36 | # TODO: Reuse load helpers method |
|---|
| | 37 | # ActionView::Base.load_helpers |
|---|
| | 38 | |
|---|
| | 39 | Dir.entries("#{File.dirname(__FILE__)}/helpers").sort.each do |file| |
|---|
| | 40 | next unless file =~ /^([a-z][a-z_]*_helper).rb$/ |
|---|
| | 41 | require "action_view/helpers/#{$1}" |
|---|
| | 42 | helper_module_name = $1.camelize |
|---|
| | 43 | if Helpers.const_defined?(helper_module_name) |
|---|
| | 44 | include Helpers.const_get(helper_module_name) |
|---|
| | 45 | end |
|---|
| | 46 | end |
|---|
| | 47 | |
|---|
| | 48 | include ActionController::PolymorphicRoutes |
|---|
| | 49 | include ActionController::RecordIdentifier |
|---|
| | 50 | |
|---|
| | 51 | def setup |
|---|
| | 52 | self.class.send(:include, helper_class) |
|---|
| | 53 | end |
|---|
| | 54 | |
|---|
| | 55 | class TestController < ActionController::Base |
|---|
| | 56 | attr_accessor :request |
|---|
| | 57 | |
|---|
| | 58 | def initialize |
|---|
| | 59 | @request = ActionController::TestRequest.new |
|---|
| | 60 | end |
|---|
| | 61 | end |
|---|
| | 62 | |
|---|
| | 63 | private |
|---|
| | 64 | def method_missing(selector, *args) |
|---|
| | 65 | controller = TestController.new |
|---|
| | 66 | return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) |
|---|
| | 67 | super |
|---|
| | 68 | end |
|---|
| | 69 | end |
|---|
| | 70 | end |