public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Introduce ActionView::TestCase for testing view helpers.
josh (author)
Sat Apr 19 11:06:57 -0700 2008
commit  17d4164a16e5fe7b252375211424a2999a331291
tree    772695335b861e70caa15d92cd77ca568406cb7f
parent  ef4c65088fb907fc819e6b5d83d284c38cdaabfc
...
38
39
40
41
42
43
 
 
 
 
...
38
39
40
 
41
 
42
43
44
45
0
@@ -38,7 +38,9 @@
0
 
0
 ActionView::Base.class_eval do
0
   include ActionView::Partials
0
-end
0
 
0
-ActionView::Base.load_helpers
0
+ ActionView::Base.helper_modules.each do |helper_module|
0
+ include helper_module
0
+ end
0
+end
...
205
206
207
208
209
 
 
 
210
211
212
213
214
 
215
216
 
217
218
219
...
323
324
325
326
 
327
328
329
...
205
206
207
 
 
208
209
210
211
212
213
214
 
215
216
217
218
219
220
221
...
325
326
327
 
328
329
330
331
0
@@ -205,15 +205,17 @@
0
     class ObjectWrapper < Struct.new(:value) #:nodoc:
0
     end
0
 
0
- def self.load_helpers #:nodoc:
0
- Dir.entries("#{File.dirname(__FILE__)}/helpers").sort.each do |file|
0
+ def self.helper_modules #:nodoc:
0
+ helpers = []
0
+ Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file|
0
         next unless file =~ /^([a-z][a-z_]*_helper).rb$/
0
         require "action_view/helpers/#{$1}"
0
         helper_module_name = $1.camelize
0
         if Helpers.const_defined?(helper_module_name)
0
- include Helpers.const_get(helper_module_name)
0
+ helpers << Helpers.const_get(helper_module_name)
0
         end
0
       end
0
+ return helpers
0
     end
0
 
0
     def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
0
@@ -323,7 +325,7 @@
0
       end
0
     end
0
 
0
- private
0
+ private
0
       def wrap_content_for_layout(content)
0
         original_content_for_layout = @content_for_layout
0
         @content_for_layout = content
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
0
@@ -1 +1,65 @@
0
+require 'active_support/test_case'
0
+
0
+module ActionView
0
+ class NonInferrableHelperError < ActionViewError
0
+ def initialize(name)
0
+ super "Unable to determine the helper to test from #{name}. " +
0
+ "You'll need to specify it using tests YourHelper in your " +
0
+ "test case definition"
0
+ end
0
+ end
0
+
0
+ class TestCase < ActiveSupport::TestCase
0
+ class_inheritable_accessor :helper_class
0
+ @@helper_class = nil
0
+
0
+ class << self
0
+ def tests(helper_class)
0
+ self.helper_class = helper_class
0
+ end
0
+
0
+ def helper_class
0
+ if current_helper_class = read_inheritable_attribute(:helper_class)
0
+ current_helper_class
0
+ else
0
+ self.helper_class = determine_default_helper_class(name)
0
+ end
0
+ end
0
+
0
+ def determine_default_helper_class(name)
0
+ name.sub(/Test$/, '').constantize
0
+ rescue NameError
0
+ raise NonInferrableHelperError.new(name)
0
+ end
0
+ end
0
+
0
+ ActionView::Base.helper_modules.each do |helper_module|
0
+ include helper_module
0
+ end
0
+ include ActionController::PolymorphicRoutes
0
+ include ActionController::RecordIdentifier
0
+
0
+ setup :setup_with_helper_class
0
+
0
+ def setup_with_helper_class
0
+ self.class.send(:include, helper_class)
0
+ end
0
+
0
+ class TestController < ActionController::Base
0
+ attr_accessor :request, :response
0
+
0
+ def initialize
0
+ @request = ActionController::TestRequest.new
0
+ @response = ActionController::TestResponse.new
0
+ end
0
+ end
0
+
0
+ private
0
+ def method_missing(selector, *args)
0
+ controller = TestController.new
0
+ return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
0
+ super
0
+ end
0
+ end
0
+end
...
8
9
10
 
11
12
13
...
8
9
10
11
12
13
14
0
@@ -8,6 +8,7 @@
0
 require 'action_controller'
0
 require 'action_controller/cgi_ext'
0
 require 'action_controller/test_process'
0
+require 'action_view/test_case'
0
 
0
 begin
0
   require 'ruby-debug'
...
1
2
3
4
5
6
7
8
9
 
 
10
11
12
...
1
2
 
 
 
 
 
 
 
3
4
5
6
7
0
@@ -1,12 +1,7 @@
0
 require 'abstract_unit'
0
 
0
-class ActiveRecordHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::FormHelper
0
- include ActionView::Helpers::ActiveRecordHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::FormTagHelper
0
+class ActiveRecordHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::ActiveRecordHelper
0
 
0
   silence_warnings do
0
     Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on)
...
1
2
3
4
5
6
 
 
7
8
9
...
445
446
447
448
449
450
451
 
 
452
453
454
...
1
2
 
 
 
 
3
4
5
6
7
...
443
444
445
 
 
 
 
446
447
448
449
450
0
@@ -1,9 +1,7 @@
0
 require 'abstract_unit'
0
 
0
-class AssetTagHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::AssetTagHelper
0
+class AssetTagHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::AssetTagHelper
0
 
0
   def setup
0
     silence_warnings do
0
@@ -445,10 +443,8 @@
0
   end
0
 end
0
 
0
-class AssetTagHelperNonVhostTest < Test::Unit::TestCase
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::AssetTagHelper
0
+class AssetTagHelperNonVhostTest < ActionView::TestCase
0
+ tests ActionView::Helpers::AssetTagHelper
0
 
0
   def setup
0
     @controller = Class.new do
...
1
2
3
4
5
 
 
6
7
8
...
1
2
3
 
 
4
5
6
7
8
0
@@ -1,8 +1,8 @@
0
 require 'abstract_unit'
0
 require 'action_view/helpers/benchmark_helper'
0
 
0
-class BenchmarkHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::BenchmarkHelper
0
+class BenchmarkHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::BenchmarkHelper
0
 
0
   class MockLogger
0
     attr_reader :logged
...
1
2
3
4
5
 
 
6
7
8
...
1
2
 
 
 
3
4
5
6
7
0
@@ -1,8 +1,7 @@
0
 require 'abstract_unit'
0
 
0
-class DateHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::DateHelper
0
- include ActionView::Helpers::FormHelper
0
+class DateHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::DateHelper
0
 
0
   silence_warnings do
0
     Post = Struct.new("Post", :id, :written_on, :updated_at)
...
30
31
32
33
34
35
36
37
38
39
40
41
 
 
42
43
44
...
30
31
32
 
 
 
 
 
 
 
 
 
33
34
35
36
37
0
@@ -30,15 +30,8 @@
0
 class Comment::Nested < Comment; end
0
 
0
 
0
-class FormHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::FormHelper
0
- include ActionView::Helpers::FormTagHelper
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::ActiveRecordHelper
0
- include ActionView::Helpers::RecordIdentificationHelper
0
- include ActionController::PolymorphicRoutes
0
+class FormHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::FormHelper
0
 
0
   def setup
0
     @post = Post.new
...
22
23
24
25
26
27
 
 
28
29
30
...
22
23
24
 
 
 
25
26
27
28
29
0
@@ -22,9 +22,8 @@
0
 
0
 ActionView::Helpers::FormOptionsHelper::TimeZone = MockTimeZone
0
 
0
-class FormOptionsHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::FormHelper
0
- include ActionView::Helpers::FormOptionsHelper
0
+class FormOptionsHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::FormOptionsHelper
0
 
0
   silence_warnings do
0
     Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin)
...
1
2
3
4
5
6
7
8
 
 
9
10
11
...
1
2
 
 
 
 
 
 
3
4
5
6
7
0
@@ -1,11 +1,7 @@
0
 require 'abstract_unit'
0
 
0
-class FormTagHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::FormTagHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::CaptureHelper
0
+class FormTagHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::FormTagHelper
0
 
0
   def setup
0
     @controller = Class.new do
...
1
2
3
4
5
6
7
8
9
10
 
 
11
12
13
...
1
2
 
 
 
 
 
 
 
 
3
4
5
6
7
0
@@ -1,13 +1,7 @@
0
 require 'abstract_unit'
0
 
0
-class JavaScriptHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::JavaScriptHelper
0
-
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::FormHelper
0
- include ActionView::Helpers::CaptureHelper
0
+class JavaScriptHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::JavaScriptHelper
0
 
0
   def test_define_javascript_functions
0
     # check if prototype.js is included first
...
1
2
3
4
 
 
5
6
7
...
1
2
 
 
3
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 require 'abstract_unit'
0
 
0
-class NumberHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::NumberHelper
0
+class NumberHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::NumberHelper
0
 
0
   def test_number_to_phone
0
     assert_equal("800-555-1212", number_to_phone(8005551212))
...
24
25
26
27
28
29
30
 
 
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
 
45
46
47
48
...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
 
 
 
 
 
 
 
 
 
 
 
 
76
77
78
79
80
 
81
82
83
...
294
295
296
297
298
299
 
300
301
302
...
24
25
26
 
 
 
 
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
33
34
35
...
46
47
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
 
 
64
65
66
67
...
278
279
280
 
 
 
281
282
283
284
0
@@ -24,24 +24,11 @@
0
 class Author::Nested < Author; end
0
 
0
 
0
-module BaseTest
0
- def self.included(base)
0
- base.send :attr_accessor, :template_format
0
- end
0
+class PrototypeHelperBaseTest < ActionView::TestCase
0
+ tests ActionView::Helpers::PrototypeHelper
0
 
0
- include ActionView::Helpers::JavaScriptHelper
0
- include ActionView::Helpers::PrototypeHelper
0
- include ActionView::Helpers::ScriptaculousHelper
0
-
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::FormTagHelper
0
- include ActionView::Helpers::FormHelper
0
- include ActionView::Helpers::CaptureHelper
0
- include ActionView::Helpers::RecordIdentificationHelper
0
- include ActionController::PolymorphicRoutes
0
-
0
+ attr_accessor :template_format
0
+
0
   def setup
0
     @template = nil
0
     @controller = Class.new do
0
0
@@ -59,25 +46,22 @@
0
     end.new
0
   end
0
 
0
-protected
0
-
0
- def request_forgery_protection_token
0
- nil
0
- end
0
-
0
- def protect_against_forgery?
0
- false
0
- end
0
-
0
- def create_generator
0
- block = Proc.new { |*args| yield *args if block_given? }
0
- JavaScriptGenerator.new self, &block
0
- end
0
+ protected
0
+ def request_forgery_protection_token
0
+ nil
0
+ end
0
+
0
+ def protect_against_forgery?
0
+ false
0
+ end
0
+
0
+ def create_generator
0
+ block = Proc.new { |*args| yield *args if block_given? }
0
+ JavaScriptGenerator.new self, &block
0
+ end
0
 end
0
 
0
-class PrototypeHelperTest < Test::Unit::TestCase
0
- include BaseTest
0
-
0
+class PrototypeHelperTest < PrototypeHelperBaseTest
0
   def setup
0
     @record = @author = Author.new
0
     @article = Article.new
0
@@ -294,9 +278,7 @@
0
     end
0
 end
0
 
0
-class JavaScriptGeneratorTest < Test::Unit::TestCase
0
- include BaseTest
0
-
0
+class JavaScriptGeneratorTest < PrototypeHelperBaseTest
0
   def setup
0
     super
0
     @generator = create_generator
...
9
10
11
12
13
14
15
16
17
18
19
 
 
 
20
21
22
...
9
10
11
 
 
 
 
 
 
 
 
12
13
14
15
16
17
0
@@ -9,14 +9,9 @@
0
   end
0
 end
0
 
0
-class RecordTagHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::RecordTagHelper
0
- include ActionView::Helpers::CaptureHelper
0
- include ActionView::Helpers::RecordIdentificationHelper
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::UrlHelper
0
-
0
+class RecordTagHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::RecordTagHelper
0
+
0
   def setup
0
     @post = Post.new
0
   end
...
3
4
5
6
7
8
 
 
9
10
11
...
3
4
5
 
 
 
6
7
8
9
10
0
@@ -3,9 +3,8 @@
0
 
0
 # The exhaustive tests are in test/controller/html/sanitizer_test.rb.
0
 # This tests the that the helpers hook up correctly to the sanitizer classes.
0
-class SanitizeHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::SanitizeHelper
0
- include ActionView::Helpers::TagHelper
0
+class SanitizeHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::SanitizeHelper
0
   include TestingSandbox
0
 
0
   def test_strip_links
...
1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
 
14
15
16
...
1
2
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
7
8
0
@@ -1,16 +1,8 @@
0
 require 'abstract_unit'
0
 
0
-class ScriptaculousHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::JavaScriptHelper
0
- include ActionView::Helpers::PrototypeHelper
0
- include ActionView::Helpers::ScriptaculousHelper
0
-
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::FormHelper
0
- include ActionView::Helpers::CaptureHelper
0
-
0
+class ScriptaculousHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::ScriptaculousHelper
0
+
0
   def setup
0
     @controller = Class.new do
0
       def url_for(options)
...
1
2
3
4
5
6
7
 
 
8
9
10
...
1
2
 
 
 
 
 
3
4
5
6
7
0
@@ -1,10 +1,7 @@
0
 require 'abstract_unit'
0
 
0
-class TagHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::CaptureHelper
0
+class TagHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::TagHelper
0
 
0
   def test_tag
0
     assert_equal "<br />", tag("br")
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
0
@@ -1 +1,57 @@
0
+require 'abstract_unit'
0
+
0
+module PeopleHelper
0
+ def title(text)
0
+ content_tag(:h1, text)
0
+ end
0
+
0
+ def homepage_path
0
+ people_path
0
+ end
0
+
0
+ def homepage_url
0
+ people_url
0
+ end
0
+
0
+ def link_to_person(person)
0
+ link_to person.name, person
0
+ end
0
+end
0
+
0
+class PeopleHelperTest < ActionView::TestCase
0
+ def setup
0
+ ActionController::Routing::Routes.draw do |map|
0
+ map.people 'people', :controller => 'people', :action => 'index'
0
+ map.connect ':controller/:action/:id'
0
+ end
0
+ end
0
+
0
+ def test_title
0
+ assert_equal "<h1>Ruby on Rails</h1>", title("Ruby on Rails")
0
+ end
0
+
0
+ def test_homepage_path
0
+ assert_equal "/people", homepage_path
0
+ end
0
+
0
+ def test_homepage_url
0
+ assert_equal "http://test.host/people", homepage_url
0
+ end
0
+
0
+ uses_mocha "link_to_person" do
0
+ def test_link_to_person
0
+ person = mock(:name => "David")
0
+ expects(:mocha_mock_path).with(person).returns("/people/1")
0
+ assert_equal '<a href="/people/1">David</a>', link_to_person(person)
0
+ end
0
+ end
0
+end
0
+
0
+class CrazyHelperTest < ActionView::TestCase
0
+ tests PeopleHelper
0
+
0
+ def test_helper_class_can_be_set_manually_not_just_inferred
0
+ assert_equal PeopleHelper, self.class.helper_class
0
+ end
0
+end
...
1
2
3
4
5
6
 
 
7
8
9
...
1
2
3
 
 
 
4
5
6
7
8
0
@@ -1,9 +1,8 @@
0
 require 'abstract_unit'
0
 require 'testing_sandbox'
0
 
0
-class TextHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::TagHelper
0
+class TextHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::TextHelper
0
   include TestingSandbox
0
 
0
   def setup
...
2
3
4
5
6
7
8
 
 
9
10
11
...
293
294
295
296
 
297
298
299
...
310
311
312
313
 
314
315
316
...
348
349
350
351
 
352
353
354
...
372
373
374
375
 
376
377
378
...
440
441
442
443
 
444
445
446
...
479
480
481
482
 
483
484
485
...
2
3
4
 
 
 
 
5
6
7
8
9
...
291
292
293
 
294
295
296
297
...
308
309
310
 
311
312
313
314
...
346
347
348
 
349
350
351
352
...
370
371
372
 
373
374
375
376
...
438
439
440
 
441
442
443
444
...
477
478
479
 
480
481
482
483
0
@@ -2,10 +2,8 @@
0
 
0
 RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port, :env)
0
 
0
-class UrlHelperTest < Test::Unit::TestCase
0
- include ActionView::Helpers::AssetTagHelper
0
- include ActionView::Helpers::UrlHelper
0
- include ActionView::Helpers::TagHelper
0
+class UrlHelperTest < ActionView::TestCase
0
+ tests ActionView::Helpers::UrlHelper
0
 
0
   def setup
0
     @controller = Class.new do
0
@@ -293,7 +291,7 @@
0
   end
0
 end
0
 
0
-class UrlHelperWithControllerTest < Test::Unit::TestCase
0
+class UrlHelperWithControllerTest < ActionView::TestCase
0
   class UrlHelperController < ActionController::Base
0
     self.view_paths = [ "#{File.dirname(__FILE__)}/../fixtures/" ]
0
 
0
@@ -310,7 +308,7 @@
0
     def rescue_action(e) raise e end
0
   end
0
 
0
- include ActionView::Helpers::UrlHelper
0
+ tests ActionView::Helpers::UrlHelper
0
 
0
   def setup
0
     @request = ActionController::TestRequest.new
0
@@ -348,7 +346,7 @@
0
     end
0
 end
0
 
0
-class LinkToUnlessCurrentWithControllerTest < Test::Unit::TestCase
0
+class LinkToUnlessCurrentWithControllerTest < ActionView::TestCase
0
   class TasksController < ActionController::Base
0
     self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
0
 
0
@@ -372,7 +370,7 @@
0
       end
0
   end
0
 
0
- include ActionView::Helpers::UrlHelper
0
+ tests ActionView::Helpers::UrlHelper
0
 
0
   def setup
0
     @request = ActionController::TestRequest.new
0
@@ -440,7 +438,7 @@
0
   end
0
 end
0
 
0
-class PolymorphicControllerTest < Test::Unit::TestCase
0
+class PolymorphicControllerTest < ActionView::TestCase
0
   class WorkshopsController < ActionController::Base
0
     self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
0
 
0
@@ -479,7 +477,7 @@
0
     def rescue_action(e) raise e end
0
   end
0
 
0
- include ActionView::Helpers::UrlHelper
0
+ tests ActionView::Helpers::UrlHelper
0
 
0
   def setup
0
     @request = ActionController::TestRequest.new

Comments

    No one has commented yet.