Permalink
Browse files

Cleanup route reloading in tests. Prefer with_routing over using Acti…

…onController::Routing::Routes directly
  • Loading branch information...
1 parent ccf28d2 commit 24ad9ae3d228acdf9aa31cf28bfe6dfb0139c247 @josh josh committed Aug 17, 2009
@@ -213,7 +213,7 @@ def initialize
self.routes = []
self.named_routes = NamedRouteCollection.new
- clear_recognize_optimized!
+ clear!
end
# Subclasses and plugins may override this method to specify a different
@@ -223,15 +223,18 @@ def builder
end
def draw
+ clear!
yield Mapper.new(self)
install_helpers
end
def clear!
routes.clear
named_routes.clear
+
@combined_regexp = nil
@routes_by_controller = nil
+
# This will force routing/recognition_optimization.rb
# to refresh optimisations.
clear_recognize_optimized!
@@ -262,7 +265,6 @@ def configuration_file
def load!
Routing.use_controllers!(nil) # Clear the controller cache so we may discover new ones
- clear!
load_routes!
end
@@ -286,10 +288,12 @@ def load_routes!
configuration_files.each { |config| load(config) }
@routes_last_modified = routes_changed_at
else
- add_route ":controller/:action/:id"
+ draw do |map|
+ map.connect ":controller/:action/:id"
+ end
end
end
-
+
def routes_changed_at
routes_changed_at = nil
@@ -51,6 +51,10 @@ class TestCase
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
end
+
+ teardown do
+ ActionController::Routing::Routes.reload!
+ end
end
end
end
@@ -70,12 +74,17 @@ class Base
class TestCase
include TestProcess
+
setup do
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
end
end
-
+
+ teardown do
+ ActionController::Routing::Routes.reload!
+ end
+
def assert_template(options = {}, message = nil)
validate_request!
@@ -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 ------------------------------------------------
@@ -177,17 +177,17 @@ def setup
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 @@ def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default
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
@@ -48,8 +48,6 @@ def setup
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 @@ def setup
def teardown
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
- ActionController::Routing::Routes.clear!
ActionController::Base.perform_caching = false
end
@@ -531,6 +531,7 @@ def setup
ActionController::Routing::Routes.draw do |map|
map.resources :customers
map.resources :quiz_stores, :has_many => :customers
+ map.connect ":controller/:action/:id"
end
end
@@ -231,18 +231,20 @@ def test_redirect_to_back_with_no_referer
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
@@ -1086,15 +1086,17 @@ def test_head_with_location_header
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
@@ -55,13 +55,15 @@ def test_rendering_xml_should_call_to_xml_if_possible
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
@@ -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
@@ -102,8 +102,7 @@ def test_possible_controllers_are_reset_on_each_load
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
@@ -123,15 +123,11 @@ def setup
@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
Oops, something went wrong.

0 comments on commit 24ad9ae

Please sign in to comment.