Cleanup route reloading in tests. Prefer with_routing over using ActionController::Routing::Routes directly

This commit is contained in:
Joshua Peek 2009-08-16 21:14:26 -05:00
parent ccf28d2499
commit 24ad9ae3d2
14 changed files with 149 additions and 141 deletions

View File

@ -213,7 +213,7 @@ module ActionController
self.routes = []
self.named_routes = NamedRouteCollection.new
clear_recognize_optimized!
clear!
end
# Subclasses and plugins may override this method to specify a different
@ -223,6 +223,7 @@ module ActionController
end
def draw
clear!
yield Mapper.new(self)
install_helpers
end
@ -230,8 +231,10 @@ module ActionController
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 @@ module ActionController
def load!
Routing.use_controllers!(nil) # Clear the controller cache so we may discover new ones
clear!
load_routes!
end
@ -286,10 +288,12 @@ module ActionController
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

View File

@ -51,6 +51,10 @@ module ActionView
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 @@ module ActionController
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!

View File

@ -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 ------------------------------------------------

View File

@ -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'
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.
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
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!
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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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'
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
assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again',
controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
end
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')
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'
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'
with_routing do |set|
set.draw do |map|
map.main '', :controller => 'posts', :format => nil
map.resources :posts
map.connect ':controller/:action/:id'
end
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!
# 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
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

View File

@ -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