From 43e0bc1c9f39ebf5b253b498b38101a4aba04a45 Mon Sep 17 00:00:00 2001
From: Joshua Peek <josh@joshpeek.com>
Date: Tue, 27 Oct 2009 19:48:35 -0500
Subject: Extract routing controller stub helper into abstract unit

---
 actionpack/test/abstract_unit.rb         |  20 +++++
 actionpack/test/dispatch/routing_test.rb | 145 ++++++++++++++-----------------
 2 files changed, 86 insertions(+), 79 deletions(-)

(limited to 'actionpack')

diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index 86c8a95a43..214d79cd87 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -105,6 +105,26 @@ class ActionController::IntegrationTest < ActiveSupport::TestCase
 
   self.app = build_app
 
+  class StubDispatcher
+    def self.new(*args)
+      lambda { |env|
+        params = env['action_dispatch.request.path_parameters']
+        controller, action = params[:controller], params[:action]
+        [200, {'Content-Type' => 'text/html'}, ["#{controller}##{action}"]]
+      }
+    end
+  end
+
+  def self.stub_controllers
+    old_dispatcher = ActionDispatch::Routing::RouteSet::Dispatcher
+    ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
+    ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, StubDispatcher }
+    yield ActionDispatch::Routing::RouteSet.new
+  ensure
+    ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
+    ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, old_dispatcher }
+  end
+
   def with_routing(&block)
     real_routes = ActionController::Routing::Routes
     ActionController::Routing.module_eval { remove_const :Routes }
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index 972bf73602..5558cf0154 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -12,112 +12,99 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
     end
   end
 
-  class Dispatcher
-    def self.new(*args)
-      lambda { |env|
-        params = env['action_dispatch.request.path_parameters']
-        controller, action = params[:controller], params[:action]
-        [200, {'Content-Type' => 'text/html'}, ["#{controller}##{action}"]]
-      }
-    end
-  end
-
-  old_dispatcher = ActionDispatch::Routing::RouteSet::Dispatcher
-  ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
-  ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, Dispatcher }
-  Routes = ActionDispatch::Routing::RouteSet.new
-  Routes.draw do |map|
-    controller :sessions do
-      get  'login', :to => :new, :as => :login
-      post 'login', :to => :create
-
-      delete 'logout', :to => :destroy, :as => :logout
-    end
-
-    match 'account/login', :to => redirect("/login")
-
-    match 'openid/login', :via => [:get, :post], :to => "openid#login"
-
-    controller(:global) do
-      match 'global/:action'
-      match 'global/export',      :to => :export, :as => :export_request
-      match 'global/hide_notice', :to => :hide_notice, :as => :hide_notice
-      match '/export/:id/:file',  :to => :export, :as => :export_download, :constraints => { :file => /.*/ }
-    end
-
-    constraints(:ip => /192\.168\.1\.\d\d\d/) do
-      get 'admin', :to => "queenbee#index"
-    end
+  stub_controllers do |routes|
+    Routes = routes
+    Routes.draw do |map|
+      controller :sessions do
+        get  'login', :to => :new, :as => :login
+        post 'login', :to => :create
+
+        delete 'logout', :to => :destroy, :as => :logout
+      end
 
-    constraints IpRestrictor do
-      get 'admin/accounts', :to => "queenbee#accounts"
-    end
+      match 'account/login', :to => redirect("/login")
 
-    resources :projects, :controller => :project do
-      resources :involvements, :attachments
+      match 'openid/login', :via => [:get, :post], :to => "openid#login"
 
-      resources :participants do
-        put :update_all, :on => :collection
+      controller(:global) do
+        match 'global/:action'
+        match 'global/export',      :to => :export, :as => :export_request
+        match 'global/hide_notice', :to => :hide_notice, :as => :hide_notice
+        match '/export/:id/:file',  :to => :export, :as => :export_download, :constraints => { :file => /.*/ }
       end
 
-      resources :companies do
-        resources :people
-        resource  :avatar
+      constraints(:ip => /192\.168\.1\.\d\d\d/) do
+        get 'admin', :to => "queenbee#index"
       end
 
-      resources :images do
-        post :revise, :on => :member
+      constraints IpRestrictor do
+        get 'admin/accounts', :to => "queenbee#accounts"
       end
 
-      resources :people do
-        namespace ":access_token" do
-          resource :avatar
+      resources :projects, :controller => :project do
+        resources :involvements, :attachments
+
+        resources :participants do
+          put :update_all, :on => :collection
         end
 
-        member do
-          put  :accessible_projects
-          post :resend, :generate_new_password
+        resources :companies do
+          resources :people
+          resource  :avatar
+        end
+
+        resources :images do
+          post :revise, :on => :member
+        end
+
+        resources :people do
+          namespace ":access_token" do
+            resource :avatar
+          end
+
+          member do
+            put  :accessible_projects
+            post :resend, :generate_new_password
+          end
         end
-      end
 
-      resources :posts do
-        get  :archive, :toggle_view, :on => :collection
-        post :preview, :on => :member
+        resources :posts do
+          get  :archive, :toggle_view, :on => :collection
+          post :preview, :on => :member
 
-        resource :subscription
+          resource :subscription
 
-        resources :comments do
-          post :preview, :on => :collection
+          resources :comments do
+            post :preview, :on => :collection
+          end
         end
       end
-    end
 
-    match 'sprockets.js', :to => SprocketsApp
+      match 'sprockets.js', :to => SprocketsApp
 
-    match 'people/:id/update', :to => 'people#update', :as => :update_person
-    match '/projects/:project_id/people/:id/update', :to => 'people#update', :as => :update_project_person
+      match 'people/:id/update', :to => 'people#update', :as => :update_person
+      match '/projects/:project_id/people/:id/update', :to => 'people#update', :as => :update_project_person
 
-    # misc
-    match 'articles/:year/:month/:day/:title', :to => "articles#show", :as => :article
+      # misc
+      match 'articles/:year/:month/:day/:title', :to => "articles#show", :as => :article
 
-    namespace :account do
-      resource :subscription, :credit, :credit_card
-    end
+      namespace :account do
+        resource :subscription, :credit, :credit_card
+      end
 
-    controller :articles do
-      scope 'articles' do
-        scope ':title', :title => /[a-z]+/, :as => :with_title do
-          match ':id', :to => :with_id
+      controller :articles do
+        scope 'articles' do
+          scope ':title', :title => /[a-z]+/, :as => :with_title do
+            match ':id', :to => :with_id
+          end
         end
       end
-    end
 
-    scope ':access_token', :constraints => { :access_token => /\w{5,5}/ } do
-      resources :rooms
+      scope ':access_token', :constraints => { :access_token => /\w{5,5}/ } do
+        resources :rooms
+      end
     end
   end
-  ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
-  ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, old_dispatcher }
 
   def app
     Routes
-- 
cgit v1.2.3