Skip to content

Commit

Permalink
Improved testing of functional test setup.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
seckar committed Jul 7, 2005
1 parent b9b6cb9 commit e3af27c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
38 changes: 37 additions & 1 deletion actionpack/lib/action_controller/test_process.rb
Expand Up @@ -72,10 +72,16 @@ def assign_parameters(controller_path, action, parameters)
extra_keys = ActionController::Routing::Routes.extra_keys(parameters) extra_keys = ActionController::Routing::Routes.extra_keys(parameters)
non_path_parameters = get? ? query_parameters : request_parameters non_path_parameters = get? ? query_parameters : request_parameters
parameters.each do |key, value| 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) if extra_keys.include?(key.to_sym)
non_path_parameters[key] = value non_path_parameters[key] = value
else else
path_parameters[key] = value.is_a?(Fixnum) ? value.to_s : value path_parameters[key.to_s] = value
end end
end end
end end
Expand Down Expand Up @@ -352,6 +358,36 @@ def method_missing(selector, *args)
return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector) return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector)
return super return super
end 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 end
end end
40 changes: 37 additions & 3 deletions actionpack/test/controller/test_test.rb
Expand Up @@ -114,10 +114,44 @@ def test_params_passing
) )
end end


def test_path_params_are_strings def test_id_converted_to_string
get :test_params, :id => 20, :foo => Object.new get :test_params, :id => 20, :foo => Object.new
@request.path_parameters.each do |key, value| assert_kind_of String, @request.path_parameters['id']
assert_kind_of String, value 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 end

assert ActionController::Routing::Routes
assert_equal routes_id, ActionController::Routing::Routes.object_id
end end
end end

0 comments on commit e3af27c

Please sign in to comment.