aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicholas Seckar <nseckar@gmail.com>2005-07-07 19:23:35 +0000
committerNicholas Seckar <nseckar@gmail.com>2005-07-07 19:23:35 +0000
commite3af27c51b3a7bb9dcbb45bce07c029ba019eba3 (patch)
tree4a232fbcfafbc63713593a7392b96bd58c955203
parentb9b6cb95d3c7a6f623c2b328ae2cf8b3e2a05ef1 (diff)
downloadrails-e3af27c51b3a7bb9dcbb45bce07c029ba019eba3.tar.gz
rails-e3af27c51b3a7bb9dcbb45bce07c029ba019eba3.tar.bz2
rails-e3af27c51b3a7bb9dcbb45bce07c029ba019eba3.zip
Improved testing of functional test setup.
Fixed functional test setup so that :controller and :action are stored into path_parameters using string keys. Added with_routing test helper which can be used to dynamically replace the current set of routes inside a unit test. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1763 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/lib/action_controller/test_process.rb38
-rw-r--r--actionpack/test/controller/test_test.rb40
2 files changed, 74 insertions, 4 deletions
diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb
index 8dd2e443ae..8e6c81b375 100644
--- a/actionpack/lib/action_controller/test_process.rb
+++ b/actionpack/lib/action_controller/test_process.rb
@@ -72,10 +72,16 @@ module ActionController #:nodoc:
extra_keys = ActionController::Routing::Routes.extra_keys(parameters)
non_path_parameters = get? ? query_parameters : request_parameters
parameters.each do |key, value|
+ if value.is_a? Fixnum
+ value = value.to_s
+ elsif value.is_a? Array
+ value = ActionController::Routing::PathComponent::Result.new(value)
+ end
+
if extra_keys.include?(key.to_sym)
non_path_parameters[key] = value
else
- path_parameters[key] = value.is_a?(Fixnum) ? value.to_s : value
+ path_parameters[key.to_s] = value
end
end
end
@@ -352,6 +358,36 @@ module Test
return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector)
return super
end
+
+ # A helper to make it easier to test different route configurations.
+ # This method temporarily replaces ActionController::Routing::Routes
+ # with a new RouteSet instance.
+ #
+ # The new instance is yielded to the passed block. Typically the block
+ # will create some routes using map.draw { map.connect ... }:
+ #
+ # with_routing do |set|
+ # set.draw { set.connect ':controller/:id/:action' }
+ # assert_equal(
+ # ['/content/10/show', {}],
+ # set.generate(:controller => 'content', :id => 10, :action => 'show')
+ # )
+ # end
+ #
+ def with_routing
+ real_routes = ActionController::Routing::Routes
+ ActionController::Routing.send :remove_const, :Routes
+
+ temporary_routes = ActionController::Routing::RouteSet.new
+ ActionController::Routing.send :const_set, :Routes, temporary_routes
+
+ yield temporary_routes
+ ensure
+ if ActionController::Routing.const_defined? :Routes
+ ActionController::Routing.send(:remove_const, :Routes)
+ end
+ ActionController::Routing.const_set(:Routes, real_routes) if real_routes
+ end
end
end
end
diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb
index 996c3c6cc2..201fe03b44 100644
--- a/actionpack/test/controller/test_test.rb
+++ b/actionpack/test/controller/test_test.rb
@@ -114,10 +114,44 @@ HTML
)
end
- def test_path_params_are_strings
+ def test_id_converted_to_string
get :test_params, :id => 20, :foo => Object.new
- @request.path_parameters.each do |key, value|
- assert_kind_of String, value
+ assert_kind_of String, @request.path_parameters['id']
+ end
+
+ def test_array_path_parameter_handled_properly
+ with_routing do |set|
+ set.draw do
+ set.connect 'file/*path', :controller => 'test_test/test', :action => 'test_params'
+ set.connect ':controller/:action/:id'
+ end
+
+ get :test_params, :path => ['hello', 'world']
+ assert_equal ['hello', 'world'], @request.path_parameters['path']
+ assert_equal 'hello/world', @request.path_parameters['path'].to_s
+ end
+ end
+
+ def test_assert_realistic_path_parameters
+ get :test_params, :id => 20, :foo => Object.new
+
+ # All elements of path_parameters should use string keys
+ @request.path_parameters.keys.each do |key|
+ assert_kind_of String, key
+ end
+ end
+
+ def test_with_routing_places_routes_back
+ assert ActionController::Routing::Routes
+ routes_id = ActionController::Routing::Routes.object_id
+
+ begin
+ with_routing { raise 'fail' }
+ fail 'Should not be here.'
+ rescue RuntimeError
end
+
+ assert ActionController::Routing::Routes
+ assert_equal routes_id, ActionController::Routing::Routes.object_id
end
end