PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/site/vendor/rails/actionpack/test/controller/base_test.rb

https://github.com/bradgessler/compass-style.org
Ruby | 217 lines | 171 code | 41 blank | 5 comment | 0 complexity | ba7a9511cda59a812ff81a5b8525e425 MD5 | raw file
Possible License(s): LGPL-2.0
  1. require 'abstract_unit'
  2. require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
  3. # Provide some controller to run the tests on.
  4. module Submodule
  5. class ContainedEmptyController < ActionController::Base
  6. end
  7. class ContainedNonEmptyController < ActionController::Base
  8. def public_action
  9. render :nothing => true
  10. end
  11. hide_action :hidden_action
  12. def hidden_action
  13. raise "Noooo!"
  14. end
  15. def another_hidden_action
  16. end
  17. hide_action :another_hidden_action
  18. end
  19. class SubclassedController < ContainedNonEmptyController
  20. hide_action :public_action # Hiding it here should not affect the superclass.
  21. end
  22. end
  23. class EmptyController < ActionController::Base
  24. end
  25. class NonEmptyController < ActionController::Base
  26. def public_action
  27. end
  28. hide_action :hidden_action
  29. def hidden_action
  30. end
  31. end
  32. class MethodMissingController < ActionController::Base
  33. hide_action :shouldnt_be_called
  34. def shouldnt_be_called
  35. raise "NO WAY!"
  36. end
  37. protected
  38. def method_missing(selector)
  39. render :text => selector.to_s
  40. end
  41. end
  42. class DefaultUrlOptionsController < ActionController::Base
  43. def default_url_options_action
  44. end
  45. def default_url_options(options = nil)
  46. { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
  47. end
  48. end
  49. class ControllerClassTests < Test::Unit::TestCase
  50. def test_controller_path
  51. assert_equal 'empty', EmptyController.controller_path
  52. assert_equal EmptyController.controller_path, EmptyController.new.controller_path
  53. assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
  54. assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
  55. end
  56. def test_controller_name
  57. assert_equal 'empty', EmptyController.controller_name
  58. assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
  59. end
  60. end
  61. class ControllerInstanceTests < Test::Unit::TestCase
  62. def setup
  63. @empty = EmptyController.new
  64. @contained = Submodule::ContainedEmptyController.new
  65. @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
  66. @non_empty_controllers = [NonEmptyController.new,
  67. Submodule::ContainedNonEmptyController.new]
  68. end
  69. def test_action_methods
  70. @empty_controllers.each do |c|
  71. hide_mocha_methods_from_controller(c)
  72. assert_equal Set.new, c.__send__(:action_methods), "#{c.controller_path} should be empty!"
  73. end
  74. @non_empty_controllers.each do |c|
  75. hide_mocha_methods_from_controller(c)
  76. assert_equal Set.new(%w(public_action)), c.__send__(:action_methods), "#{c.controller_path} should not be empty!"
  77. end
  78. end
  79. protected
  80. # Mocha adds some public instance methods to Object that would be
  81. # considered actions, so explicitly hide_action them.
  82. def hide_mocha_methods_from_controller(controller)
  83. mocha_methods = [
  84. :expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
  85. :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
  86. ]
  87. controller.class.__send__(:hide_action, *mocha_methods)
  88. end
  89. end
  90. class PerformActionTest < ActionController::TestCase
  91. class MockLogger
  92. attr_reader :logged
  93. def initialize
  94. @logged = []
  95. end
  96. def method_missing(method, *args)
  97. @logged << args.first
  98. end
  99. end
  100. def use_controller(controller_class)
  101. @controller = controller_class.new
  102. # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
  103. # a more accurate simulation of what happens in "real life".
  104. @controller.logger = Logger.new(nil)
  105. @request = ActionController::TestRequest.new
  106. @response = ActionController::TestResponse.new
  107. @request.host = "www.nextangle.com"
  108. rescue_action_in_public!
  109. end
  110. def test_get_on_priv_should_show_selector
  111. use_controller MethodMissingController
  112. get :shouldnt_be_called
  113. assert_response :success
  114. assert_equal 'shouldnt_be_called', @response.body
  115. end
  116. def test_method_missing_is_not_an_action_name
  117. use_controller MethodMissingController
  118. assert ! @controller.__send__(:action_methods).include?('method_missing')
  119. get :method_missing
  120. assert_response :success
  121. assert_equal 'method_missing', @response.body
  122. end
  123. def test_get_on_hidden_should_fail
  124. use_controller NonEmptyController
  125. get :hidden_action
  126. assert_response 404
  127. get :another_hidden_action
  128. assert_response 404
  129. end
  130. def test_namespaced_action_should_log_module_name
  131. use_controller Submodule::ContainedNonEmptyController
  132. @controller.logger = MockLogger.new
  133. get :public_action
  134. assert_match /Processing\sSubmodule::ContainedNonEmptyController#public_action/, @controller.logger.logged[1]
  135. end
  136. end
  137. class DefaultUrlOptionsTest < ActionController::TestCase
  138. tests DefaultUrlOptionsController
  139. def setup
  140. @request.host = 'www.example.com'
  141. rescue_action_in_public!
  142. end
  143. def test_default_url_options_are_used_if_set
  144. ActionController::Routing::Routes.draw do |map|
  145. map.default_url_options 'default_url_options', :controller => 'default_url_options'
  146. map.connect ':controller/:action/:id'
  147. end
  148. get :default_url_options_action # Make a dummy request so that the controller is initialized properly.
  149. assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
  150. assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
  151. ensure
  152. ActionController::Routing::Routes.load!
  153. end
  154. end
  155. class EmptyUrlOptionsTest < ActionController::TestCase
  156. tests NonEmptyController
  157. def setup
  158. @request.host = 'www.example.com'
  159. rescue_action_in_public!
  160. end
  161. def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
  162. get :public_action
  163. assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
  164. end
  165. end
  166. class EnsureNamedRoutesWorksTicket22BugTest < Test::Unit::TestCase
  167. def test_named_routes_still_work
  168. ActionController::Routing::Routes.draw do |map|
  169. map.resources :things
  170. end
  171. EmptyController.send :include, ActionController::UrlWriter
  172. assert_equal '/things', EmptyController.new.send(:things_path)
  173. ensure
  174. ActionController::Routing::Routes.load!
  175. end
  176. end