From 24ad9ae3d228acdf9aa31cf28bfe6dfb0139c247 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 16 Aug 2009 21:14:26 -0500 Subject: Cleanup route reloading in tests. Prefer with_routing over using ActionController::Routing::Routes directly --- .../test/controller/action_pack_assertions_test.rb | 8 +- actionpack/test/controller/base_test.rb | 34 ++--- actionpack/test/controller/caching_test.rb | 3 - actionpack/test/controller/mime_responds_test.rb | 1 + actionpack/test/controller/redirect_test.rb | 24 ++-- actionpack/test/controller/render_test.rb | 18 +-- actionpack/test/controller/render_xml_test.rb | 14 +- .../controller/request_forgery_protection_test.rb | 6 +- actionpack/test/controller/routing_test.rb | 3 +- actionpack/test/controller/test_test.rb | 6 +- actionpack/test/controller/url_rewriter_test.rb | 141 +++++++++++---------- actionpack/test/controller/verification_test.rb | 5 +- 12 files changed, 129 insertions(+), 134 deletions(-) (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index ecbaba39d1..06827e5fbc 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -185,13 +185,9 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase # let's get this party started def setup super - ActionController::Routing::Routes.reload - ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user)) - end - def teardown - super - ActionController::Routing::Routes.reload + ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user)) + ActionController::Routing::Routes.load_routes! end # -- assertion-based testing ------------------------------------------------ diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 8877057070..b97ceb4594 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -177,17 +177,17 @@ class DefaultUrlOptionsTest < ActionController::TestCase end def test_default_url_options_are_used_if_set - ActionController::Routing::Routes.draw do |map| - map.default_url_options 'default_url_options', :controller => 'default_url_options' - map.connect ':controller/:action/:id' - end + with_routing do |set| + set.draw do |map| + map.default_url_options 'default_url_options', :controller => 'default_url_options' + map.connect ':controller/:action/:id' + end - get :default_url_options_action # Make a dummy request so that the controller is initialized properly. + get :default_url_options_action # Make a dummy request so that the controller is initialized properly. - assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options') - assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url) - ensure - ActionController::Routing::Routes.load! + assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options') + assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url) + end end end @@ -206,15 +206,15 @@ class EmptyUrlOptionsTest < ActionController::TestCase end end -class EnsureNamedRoutesWorksTicket22BugTest < Test::Unit::TestCase +class EnsureNamedRoutesWorksTicket22BugTest < ActionController::TestCase def test_named_routes_still_work - ActionController::Routing::Routes.draw do |map| - map.resources :things - end - EmptyController.send :include, ActionController::UrlWriter + with_routing do |set| + set.draw do |map| + map.resources :things + end + EmptyController.send :include, ActionController::UrlWriter - assert_equal '/things', EmptyController.new.send(:things_path) - ensure - ActionController::Routing::Routes.load! + assert_equal '/things', EmptyController.new.send(:things_path) + end end end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index f1c93e6b1e..82c790bc19 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -48,8 +48,6 @@ class PageCachingTest < ActionController::TestCase super ActionController::Base.perform_caching = true - ActionController::Routing::Routes.clear! - ActionController::Routing::Routes.draw do |map| map.main '', :controller => 'posts', :format => nil map.formatted_posts 'posts.:format', :controller => 'posts' @@ -72,7 +70,6 @@ class PageCachingTest < ActionController::TestCase def teardown FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) - ActionController::Routing::Routes.clear! ActionController::Base.perform_caching = false end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 3f00b9ba2f..44536ce54e 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -531,6 +531,7 @@ class RespondWithControllerTest < ActionController::TestCase ActionController::Routing::Routes.draw do |map| map.resources :customers map.resources :quiz_stores, :has_many => :customers + map.connect ":controller/:action/:id" end end diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb index 7755af592d..ea278fd8f0 100644 --- a/actionpack/test/controller/redirect_test.rb +++ b/actionpack/test/controller/redirect_test.rb @@ -231,18 +231,20 @@ class RedirectTest < ActionController::TestCase end def test_redirect_to_record - ActionController::Routing::Routes.draw do |map| - map.resources :workshops - map.connect ':controller/:action/:id' + with_routing do |set| + set.draw do |map| + map.resources :workshops + map.connect ':controller/:action/:id' + end + + get :redirect_to_existing_record + assert_equal "http://test.host/workshops/5", redirect_to_url + assert_redirected_to Workshop.new(5, false) + + get :redirect_to_new_record + assert_equal "http://test.host/workshops", redirect_to_url + assert_redirected_to Workshop.new(5, true) end - - get :redirect_to_existing_record - assert_equal "http://test.host/workshops/5", redirect_to_url - assert_redirected_to Workshop.new(5, false) - - get :redirect_to_new_record - assert_equal "http://test.host/workshops", redirect_to_url - assert_redirected_to Workshop.new(5, true) end def test_redirect_to_nil diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 9c5b560c2c..abcc8bf384 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -1086,15 +1086,17 @@ class RenderTest < ActionController::TestCase end def test_head_with_location_object - ActionController::Routing::Routes.draw do |map| - map.resources :customers - map.connect ':controller/:action/:id' - end + with_routing do |set| + set.draw do |map| + map.resources :customers + map.connect ':controller/:action/:id' + end - get :head_with_location_object - assert @response.body.blank? - assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"] - assert_response :ok + get :head_with_location_object + assert @response.body.blank? + assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"] + assert_response :ok + end end def test_head_with_custom_header diff --git a/actionpack/test/controller/render_xml_test.rb b/actionpack/test/controller/render_xml_test.rb index 139f55d8bd..e96e8a4d57 100644 --- a/actionpack/test/controller/render_xml_test.rb +++ b/actionpack/test/controller/render_xml_test.rb @@ -55,13 +55,15 @@ class RenderTest < ActionController::TestCase end def test_rendering_with_object_location_should_set_header_with_url_for - ActionController::Routing::Routes.draw do |map| - map.resources :customers - map.connect ':controller/:action/:id' - end + with_routing do |set| + set.draw do |map| + map.resources :customers + map.connect ':controller/:action/:id' + end - get :render_with_object_location - assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"] + get :render_with_object_location + assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"] + end end def test_should_render_formatted_xml_erb_template diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index 83925ed4db..7111796f8d 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -1,10 +1,6 @@ require 'abstract_unit' require 'digest/sha1' -ActionController::Routing::Routes.draw do |map| - map.connect ':controller/:action/:id' -end - # common controller actions module RequestForgeryProtectionActions def index @@ -50,7 +46,7 @@ module RequestForgeryProtectionTests def teardown ActionController::Base.request_forgery_protection_token = nil end - + def test_should_render_form_with_token_tag get :index diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 33fb6ac017..5be780dd42 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -102,8 +102,7 @@ class RoutingTest < Test::Unit::TestCase ActionController::Routing.use_controllers! true_possible_controllers Object.send(:remove_const, :RAILS_ROOT) rescue nil - ActionController::Routing::Routes.clear! - ActionController::Routing::Routes.load_routes! + ActionController::Routing::Routes.reload! end def test_with_controllers diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb index 9e88188b9a..84c97851ee 100644 --- a/actionpack/test/controller/test_test.rb +++ b/actionpack/test/controller/test_test.rb @@ -123,15 +123,11 @@ XML @controller = TestController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + ActionController::Routing.use_controllers! %w(content admin/user test_test/test) ActionController::Routing::Routes.load_routes! end - def teardown - super - ActionController::Routing::Routes.reload - end - def test_raw_post_handling params = {:page => {:name => 'page name'}, 'some key' => 123} post :render_raw_post, params.dup diff --git a/actionpack/test/controller/url_rewriter_test.rb b/actionpack/test/controller/url_rewriter_test.rb index 0e149cf8ae..ad915e7a57 100644 --- a/actionpack/test/controller/url_rewriter_test.rb +++ b/actionpack/test/controller/url_rewriter_test.rb @@ -195,61 +195,62 @@ class UrlWriterTests < ActionController::TestCase end def test_named_routes - ActionController::Routing::Routes.draw do |map| - map.no_args '/this/is/verbose', :controller => 'home', :action => 'index' - map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index' - map.connect ':controller/:action/:id' + with_routing do |set| + set.draw do |map| + map.no_args '/this/is/verbose', :controller => 'home', :action => 'index' + map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index' + map.connect ':controller/:action/:id' + end + + # We need to create a new class in order to install the new named route. + kls = Class.new { include ActionController::UrlWriter } + controller = kls.new + assert controller.respond_to?(:home_url) + assert_equal 'http://www.basecamphq.com/home/sweet/home/again', + controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again') + + assert_equal("/home/sweet/home/alabama", controller.send(:home_path, :user => 'alabama', :host => 'unused')) + assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'www.basecamphq.com')) + assert_equal("http://www.basecamphq.com/this/is/verbose", controller.send(:no_args_url, :host=>'www.basecamphq.com')) end - - # We need to create a new class in order to install the new named route. - kls = Class.new { include ActionController::UrlWriter } - controller = kls.new - assert controller.respond_to?(:home_url) - assert_equal 'http://www.basecamphq.com/home/sweet/home/again', - controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again') - - assert_equal("/home/sweet/home/alabama", controller.send(:home_path, :user => 'alabama', :host => 'unused')) - assert_equal("http://www.basecamphq.com/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'www.basecamphq.com')) - assert_equal("http://www.basecamphq.com/this/is/verbose", controller.send(:no_args_url, :host=>'www.basecamphq.com')) - ensure - ActionController::Routing::Routes.load! end def test_relative_url_root_is_respected_for_named_routes orig_relative_url_root = ActionController::Base.relative_url_root ActionController::Base.relative_url_root = '/subdir' - ActionController::Routing::Routes.draw do |map| - map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index' - end + with_routing do |set| + set.draw do |map| + map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index' + end - kls = Class.new { include ActionController::UrlWriter } - controller = kls.new + kls = Class.new { include ActionController::UrlWriter } + controller = kls.new - assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again', - controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again') + assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again', + controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again') + end ensure - ActionController::Routing::Routes.load! ActionController::Base.relative_url_root = orig_relative_url_root end def test_only_path - ActionController::Routing::Routes.draw do |map| - map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index' - map.connect ':controller/:action/:id' + with_routing do |set| + set.draw do |map| + map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index' + map.connect ':controller/:action/:id' + end + + # We need to create a new class in order to install the new named route. + kls = Class.new { include ActionController::UrlWriter } + controller = kls.new + assert controller.respond_to?(:home_url) + assert_equal '/brave/new/world', + controller.send(:url_for, :controller => 'brave', :action => 'new', :id => 'world', :only_path => true) + + assert_equal("/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'unused', :only_path => true)) + assert_equal("/home/sweet/home/alabama", controller.send(:home_path, 'alabama')) end - - # We need to create a new class in order to install the new named route. - kls = Class.new { include ActionController::UrlWriter } - controller = kls.new - assert controller.respond_to?(:home_url) - assert_equal '/brave/new/world', - controller.send(:url_for, :controller => 'brave', :action => 'new', :id => 'world', :only_path => true) - - assert_equal("/home/sweet/home/alabama", controller.send(:home_url, :user => 'alabama', :host => 'unused', :only_path => true)) - assert_equal("/home/sweet/home/alabama", controller.send(:home_path, 'alabama')) - ensure - ActionController::Routing::Routes.load! end def test_one_parameter @@ -302,41 +303,41 @@ class UrlWriterTests < ActionController::TestCase end def test_named_routes_with_nil_keys - ActionController::Routing::Routes.clear! - ActionController::Routing::Routes.draw do |map| - map.main '', :controller => 'posts', :format => nil - map.resources :posts - map.connect ':controller/:action/:id' + with_routing do |set| + set.draw do |map| + map.main '', :controller => 'posts', :format => nil + map.resources :posts + map.connect ':controller/:action/:id' + end + + # We need to create a new class in order to install the new named route. + kls = Class.new { include ActionController::UrlWriter } + kls.default_url_options[:host] = 'www.basecamphq.com' + + controller = kls.new + params = {:action => :index, :controller => :posts, :format => :xml} + assert_equal("http://www.basecamphq.com/posts.xml", controller.send(:url_for, params)) + params[:format] = nil + assert_equal("http://www.basecamphq.com/", controller.send(:url_for, params)) end - # We need to create a new class in order to install the new named route. - kls = Class.new { include ActionController::UrlWriter } - kls.default_url_options[:host] = 'www.basecamphq.com' - - controller = kls.new - params = {:action => :index, :controller => :posts, :format => :xml} - assert_equal("http://www.basecamphq.com/posts.xml", controller.send(:url_for, params)) - params[:format] = nil - assert_equal("http://www.basecamphq.com/", controller.send(:url_for, params)) - ensure - ActionController::Routing::Routes.load! end def test_formatted_url_methods_are_deprecated - ActionController::Routing::Routes.draw do |map| - map.resources :posts + with_routing do |set| + set.draw do |map| + map.resources :posts + end + # We need to create a new class in order to install the new named route. + kls = Class.new { include ActionController::UrlWriter } + controller = kls.new + params = {:id => 1, :format => :xml} + assert_deprecated do + assert_equal("/posts/1.xml", controller.send(:formatted_post_path, params)) + end + assert_deprecated do + assert_equal("/posts/1.xml", controller.send(:formatted_post_path, 1, :xml)) + end end - # We need to create a new class in order to install the new named route. - kls = Class.new { include ActionController::UrlWriter } - controller = kls.new - params = {:id => 1, :format => :xml} - assert_deprecated do - assert_equal("/posts/1.xml", controller.send(:formatted_post_path, params)) - end - assert_deprecated do - assert_equal("/posts/1.xml", controller.send(:formatted_post_path, 1, :xml)) - end - ensure - ActionController::Routing::Routes.load! end def test_multiple_includes_maintain_distinct_options diff --git a/actionpack/test/controller/verification_test.rb b/actionpack/test/controller/verification_test.rb index d568030e41..ee558f3465 100644 --- a/actionpack/test/controller/verification_test.rb +++ b/actionpack/test/controller/verification_test.rb @@ -112,7 +112,10 @@ class VerificationTest < ActionController::TestCase tests TestController setup do - ActionController::Routing::Routes.add_named_route :foo, '/foo', :controller => 'test', :action => 'foo' + ActionController::Routing::Routes.draw do |map| + map.foo '/foo', :controller => 'test', :action => 'foo' + map.connect ":controller/:action/:id" + end end def test_using_symbol_back_with_no_referrer -- cgit v1.2.3