mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor route assertion methods in resources test
The tests and methods were hard to read with `options[:options]` all over the place. This refactoring makes the code easier to understand. The change came out of work for moving the underlying code of controller tests to integraiton tests.
This commit is contained in:
parent
d919fd88ec
commit
286cb890de
1 changed files with 51 additions and 50 deletions
|
@ -1128,14 +1128,14 @@ class ResourcesTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_restful_routes_for(controller_name, options = {})
|
def assert_restful_routes_for(controller_name, options = {})
|
||||||
options[:options] ||= {}
|
route_options = (options[:options] ||= {}).dup
|
||||||
options[:options][:controller] = options[:controller] || controller_name.to_s
|
route_options[:controller] = options[:controller] || controller_name.to_s
|
||||||
|
|
||||||
if options[:shallow]
|
if options[:shallow]
|
||||||
options[:shallow_options] ||= {}
|
options[:shallow_options] ||= {}
|
||||||
options[:shallow_options][:controller] = options[:options][:controller]
|
options[:shallow_options][:controller] = route_options[:controller]
|
||||||
else
|
else
|
||||||
options[:shallow_options] = options[:options]
|
options[:shallow_options] = route_options
|
||||||
end
|
end
|
||||||
|
|
||||||
new_action = @routes.resources_path_names[:new] || "new"
|
new_action = @routes.resources_path_names[:new] || "new"
|
||||||
|
@ -1154,7 +1154,7 @@ class ResourcesTest < ActionController::TestCase
|
||||||
edit_member_path = "#{member_path}/#{edit_action}"
|
edit_member_path = "#{member_path}/#{edit_action}"
|
||||||
formatted_edit_member_path = "#{member_path}/#{edit_action}.xml"
|
formatted_edit_member_path = "#{member_path}/#{edit_action}.xml"
|
||||||
|
|
||||||
with_options(options[:options]) do |controller|
|
with_options(route_options) do |controller|
|
||||||
controller.assert_routing collection_path, :action => 'index'
|
controller.assert_routing collection_path, :action => 'index'
|
||||||
controller.assert_routing new_path, :action => 'new'
|
controller.assert_routing new_path, :action => 'new'
|
||||||
controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
|
controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
|
||||||
|
@ -1168,23 +1168,23 @@ class ResourcesTest < ActionController::TestCase
|
||||||
controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
|
controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_recognizes(options[:options].merge(:action => 'index'), :path => collection_path, :method => :get)
|
assert_recognizes(route_options.merge(:action => 'index'), :path => collection_path, :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
|
assert_recognizes(route_options.merge(:action => 'new'), :path => new_path, :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'create'), :path => collection_path, :method => :post)
|
assert_recognizes(route_options.merge(:action => 'create'), :path => collection_path, :method => :post)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
|
assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
|
assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
|
assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
|
assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
|
||||||
|
|
||||||
assert_recognizes(options[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
|
assert_recognizes(route_options.merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
assert_recognizes(route_options.merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
|
assert_recognizes(route_options.merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
|
assert_recognizes(options[:shallow_options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
|
assert_recognizes(options[:shallow_options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
|
assert_recognizes(options[:shallow_options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
|
||||||
assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
|
assert_recognizes(options[:shallow_options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
|
||||||
|
|
||||||
yield options[:options] if block_given?
|
yield route_options if block_given?
|
||||||
end
|
end
|
||||||
|
|
||||||
# test named routes like foo_path and foos_path map to the correct options.
|
# test named routes like foo_path and foos_path map to the correct options.
|
||||||
|
@ -1195,20 +1195,20 @@ class ResourcesTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
singular_name ||= controller_name.to_s.singularize
|
singular_name ||= controller_name.to_s.singularize
|
||||||
|
|
||||||
options[:options] ||= {}
|
route_options = (options[:options] ||= {}).dup
|
||||||
options[:options][:controller] = options[:controller] || controller_name.to_s
|
route_options[:controller] = options[:controller] || controller_name.to_s
|
||||||
|
|
||||||
if options[:shallow]
|
if options[:shallow]
|
||||||
options[:shallow_options] ||= {}
|
options[:shallow_options] ||= {}
|
||||||
options[:shallow_options][:controller] = options[:options][:controller]
|
options[:shallow_options][:controller] = route_options[:controller]
|
||||||
else
|
else
|
||||||
options[:shallow_options] = options[:options]
|
options[:shallow_options] = route_options
|
||||||
end
|
end
|
||||||
|
|
||||||
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new
|
@controller = "#{route_options[:controller].camelize}Controller".constantize.new
|
||||||
@controller.singleton_class.include(@routes.url_helpers)
|
@controller.singleton_class.include(@routes.url_helpers)
|
||||||
get :index, params: options[:options]
|
get :index, params: route_options
|
||||||
options[:options].delete :action
|
route_options.delete :action
|
||||||
|
|
||||||
path = "#{options[:as] || controller_name}"
|
path = "#{options[:as] || controller_name}"
|
||||||
shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
|
shallow_path = "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
|
||||||
|
@ -1223,29 +1223,29 @@ class ResourcesTest < ActionController::TestCase
|
||||||
edit_action = options[:path_names][:edit] || "edit"
|
edit_action = options[:path_names][:edit] || "edit"
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_named_route "#{full_path}", "#{name_prefix}#{controller_name}_path", options[:options]
|
assert_named_route "#{full_path}", "#{name_prefix}#{controller_name}_path", route_options
|
||||||
assert_named_route "#{full_path}.xml", "#{name_prefix}#{controller_name}_path", options[:options].merge(:format => 'xml')
|
assert_named_route "#{full_path}.xml", "#{name_prefix}#{controller_name}_path", route_options.merge(:format => 'xml')
|
||||||
assert_named_route "#{shallow_path}/1", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
|
assert_named_route "#{shallow_path}/1", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
|
||||||
assert_named_route "#{shallow_path}/1.xml", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
|
assert_named_route "#{shallow_path}/1.xml", "#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
|
||||||
|
|
||||||
assert_named_route "#{full_path}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options[:options]
|
assert_named_route "#{full_path}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", route_options
|
||||||
assert_named_route "#{full_path}/#{new_action}.xml", "new_#{name_prefix}#{singular_name}_path", options[:options].merge(:format => 'xml')
|
assert_named_route "#{full_path}/#{new_action}.xml", "new_#{name_prefix}#{singular_name}_path", route_options.merge(:format => 'xml')
|
||||||
assert_named_route "#{shallow_path}/1/#{edit_action}", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
|
assert_named_route "#{shallow_path}/1/#{edit_action}", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1')
|
||||||
assert_named_route "#{shallow_path}/1/#{edit_action}.xml", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
|
assert_named_route "#{shallow_path}/1/#{edit_action}.xml", "edit_#{shallow_prefix}#{singular_name}_path", options[:shallow_options].merge(:id => '1', :format => 'xml')
|
||||||
|
|
||||||
yield options[:options] if block_given?
|
yield route_options if block_given?
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_singleton_routes_for(singleton_name, options = {})
|
def assert_singleton_routes_for(singleton_name, options = {})
|
||||||
options[:options] ||= {}
|
route_options = (options[:options] ||= {}).dup
|
||||||
options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
|
route_options[:controller] = options[:controller] || singleton_name.to_s.pluralize
|
||||||
|
|
||||||
full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
|
full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
|
||||||
new_path = "#{full_path}/new"
|
new_path = "#{full_path}/new"
|
||||||
edit_path = "#{full_path}/edit"
|
edit_path = "#{full_path}/edit"
|
||||||
formatted_edit_path = "#{full_path}/edit.xml"
|
formatted_edit_path = "#{full_path}/edit.xml"
|
||||||
|
|
||||||
with_options options[:options] do |controller|
|
with_options route_options do |controller|
|
||||||
controller.assert_routing full_path, :action => 'show'
|
controller.assert_routing full_path, :action => 'show'
|
||||||
controller.assert_routing new_path, :action => 'new'
|
controller.assert_routing new_path, :action => 'new'
|
||||||
controller.assert_routing edit_path, :action => 'edit'
|
controller.assert_routing edit_path, :action => 'edit'
|
||||||
|
@ -1254,40 +1254,41 @@ class ResourcesTest < ActionController::TestCase
|
||||||
controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
|
controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get)
|
assert_recognizes(route_options.merge(:action => 'show'), :path => full_path, :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
|
assert_recognizes(route_options.merge(:action => 'new'), :path => new_path, :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get)
|
assert_recognizes(route_options.merge(:action => 'edit'), :path => edit_path, :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
|
assert_recognizes(route_options.merge(:action => 'create'), :path => full_path, :method => :post)
|
||||||
assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
|
assert_recognizes(route_options.merge(:action => 'update'), :path => full_path, :method => :put)
|
||||||
assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
|
assert_recognizes(route_options.merge(:action => 'destroy'), :path => full_path, :method => :delete)
|
||||||
|
|
||||||
assert_recognizes(options[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
|
assert_recognizes(route_options.merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
assert_recognizes(route_options.merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
|
assert_recognizes(route_options.merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
|
||||||
assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
|
assert_recognizes(route_options.merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
|
||||||
assert_recognizes(options[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
|
assert_recognizes(route_options.merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
|
||||||
assert_recognizes(options[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
|
assert_recognizes(route_options.merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
|
||||||
|
|
||||||
yield options[:options] if block_given?
|
yield route_options if block_given?
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_singleton_named_routes_for(singleton_name, options = {})
|
def assert_singleton_named_routes_for(singleton_name, options = {})
|
||||||
(options[:options] ||= {})[:controller] ||= singleton_name.to_s.pluralize
|
route_options = (options[:options] ||= {}).dup
|
||||||
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new
|
controller_name = route_options[:controller] || options[:controller] || singleton_name.to_s.pluralize
|
||||||
|
@controller = "#{controller_name.camelize}Controller".constantize.new
|
||||||
@controller.singleton_class.include(@routes.url_helpers)
|
@controller.singleton_class.include(@routes.url_helpers)
|
||||||
get :show, params: options[:options]
|
get :show, params: route_options
|
||||||
options[:options].delete :action
|
route_options.delete :action
|
||||||
|
|
||||||
full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
|
full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
|
||||||
name_prefix = options[:name_prefix]
|
name_prefix = options[:name_prefix]
|
||||||
|
|
||||||
assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
|
assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", route_options
|
||||||
assert_named_route "#{full_path}.xml", "#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
assert_named_route "#{full_path}.xml", "#{name_prefix}#{singleton_name}_path", route_options.merge(:format => 'xml')
|
||||||
|
|
||||||
assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
|
assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", route_options
|
||||||
assert_named_route "#{full_path}/new.xml", "new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
assert_named_route "#{full_path}/new.xml", "new_#{name_prefix}#{singleton_name}_path", route_options.merge(:format => 'xml')
|
||||||
assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
|
assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", route_options
|
||||||
assert_named_route "#{full_path}/edit.xml", "edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
|
assert_named_route "#{full_path}/edit.xml", "edit_#{name_prefix}#{singleton_name}_path", route_options.merge(:format => 'xml')
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_named_route(expected, route, options)
|
def assert_named_route(expected, route, options)
|
||||||
|
|
Loading…
Reference in a new issue