Movatterモバイル変換


[0]ホーム

URL:


Skip to ContentSkip to Search
Ruby on Rails 8.1.1

Module ActionDispatch::Assertions::RoutingAssertions

v8.1.1

Suite of assertions to test routes generated byRails and the handling of requests made to them.

Namespace
Methods
A
M
W

Instance Public methods

assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil)Link

Asserts that the provided options can be used to generate the provided path. This is the inverse ofassert_recognizes. Theextras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. Themessage parameter allows you to specify a custom error message for assertion failures.

Thedefaults parameter is unused.

# Asserts that the default action is generated for a route with no actionassert_generates"/items",controller:"items",action:"index"# Tests that the list action is properly routedassert_generates"/items/list",controller:"items",action:"list"# Tests the generation of a route with a parameterassert_generates"/items/list/1", {controller:"items",action:"list",id:"1" }# Asserts that the generated route gives us our custom routeassert_generates"changesets/12", {controller:'scm',action:'show_diff',revision:"12" }

Source:show |on GitHub

# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 216defassert_generates(expected_path,options,defaults = {},extras = {},message =nil)ifexpected_path.include?("://")fail_on(URI::InvalidURIError,message)douri =URI.parse(expected_path)expected_path =uri.path.to_s.empty??"/":uri.pathendelseexpected_path ="/#{expected_path}"unlessexpected_path.start_with?("/")endoptions =options.clonegenerated_path,query_string_keys =@routes.generate_extras(options,defaults)found_extras =options.reject {|k,_|!query_string_keys.include?k }msg =message||sprintf("found extras <%s>, not <%s>",found_extras,extras)assert_equal(extras,found_extras,msg)msg =message||sprintf("The generated path <%s> did not match <%s>",generated_path,expected_path)assert_equal(expected_path,generated_path,msg)end

assert_recognizes(expected_options, path, extras = {}, msg = nil)Link

Asserts that the routing of the givenpath was handled correctly and that the parsed options (given in theexpected_options hash) matchpath. Basically, it asserts thatRails recognizes the route given byexpected_options.

Pass a hash in the second argument (path) to specify the request method. This is useful for routes requiring a specific HTTP method. The hash should contain a:path with the incoming request path and a:method containing the required HTTP verb.

# Asserts that POSTing to /items will call the create action on ItemsControllerassert_recognizes({controller:'items',action:'create'}, {path:'items',method::post})

You can also pass inextras with a hash containing URL parameters that would normally be in the query string. This can be used to assert that values in the query string will end up in the params hash correctly. To test query strings you must use the extras argument because appending the query string on the path directly will not work. For example:

# Asserts that a path of '/items/list/1?view=print' returns the correct optionsassert_recognizes({controller:'items',action:'list',id:'1',view:'print'},'items/list/1', {view:"print" })

Themessage parameter allows you to pass in an error message that is displayed upon failure.

# Check the default route (i.e., the index action)assert_recognizes({controller:'items',action:'index'},'items')# Test a specific actionassert_recognizes({controller:'items',action:'list'},'items/list')# Test an action with a parameterassert_recognizes({controller:'items',action:'destroy',id:'1'},'items/destroy/1')# Test a custom routeassert_recognizes({controller:'items',action:'show',id:'1'},'view/item1')

Source:show |on GitHub

# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 176defassert_recognizes(expected_options,path,extras = {},msg =nil)ifpath.is_a?(Hash)&&path[:method].to_s=="all"    [:get,:post,:put,:delete].eachdo|method|assert_recognizes(expected_options,path.merge(method:method),extras,msg)endelserequest =recognized_request_for(path,extras,msg)expected_options =expected_options.cloneexpected_options.stringify_keys!msg =message(msg,"") {sprintf("The recognized options <%s> did not match <%s>, difference:",request.path_parameters,expected_options)    }assert_equal(expected_options,request.path_parameters,msg)endend

assert_routing(path, options, defaults = {}, extras = {}, message = nil)Link

Asserts that path and options match both ways; in other words, it verifies thatpath generatesoptions and then thatoptions generatespath. This essentially combinesassert_recognizes andassert_generates into one step.

Theextras hash allows you to specify options that would normally be provided as a query string to the action. Themessage parameter allows you to specify a custom error message to display upon failure.

# Asserts a basic route: a controller with the default action (index)assert_routing'/home',controller:'home',action:'index'# Test a route generated with a specific controller, action, and parameter (id)assert_routing'/entries/show/23',controller:'entries',action:'show',id:23# Asserts a basic route (controller + default action), with an error message if it failsassert_routing'/store', {controller:'store',action:'index' }, {}, {},'Route for store index not generated properly'# Tests a route, providing a defaults hashassert_routing'controller/action/9', {id:"9",item:"square"}, {controller:"controller",action:"action"}, {}, {item:"square"}# Tests a route with an HTTP methodassert_routing({method:'put',path:'/product/321' }, {controller:"product",action:"update",id:"321" })

Source:show |on GitHub

# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 260defassert_routing(path,options,defaults = {},extras = {},message =nil)assert_recognizes(options,path,extras,message)controller,default_controller =options[:controller],defaults[:controller]ifcontroller&&controller.include?(?/)&&default_controller&&default_controller.include?(?/)options[:controller] ="/#{controller}"endgenerate_options =options.dup.delete_if {|k,_|defaults.key?(k) }assert_generates(path.is_a?(Hash)?path[:path]:path,generate_options,defaults,extras,message)end

method_missing(selector, ...)Link

ROUTES TODO: These assertions should really work in an integration context

Source:show |on GitHub

# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 273defmethod_missing(selector,...)if@controller&&@routes&.named_routes&.route_defined?(selector)@controller.public_send(selector,...)elsesuperendend

with_routing(config = nil, &block)Link

A helper to make it easier to test different route configurations. This method temporarily replaces @routes with a new RouteSet instance.

The new instance is yielded to the passed block. Typically the block will create some routes usingset.draw { match ... }:

with_routingdo|set|set.drawdoresources:usersendassert_equal"/users",users_pathend

Source:show |on GitHub

# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 133defwith_routing(config =nil,&block)old_routes,old_controller =@routes,@controllercreate_routes(config,&block)ensurereset_routes(old_routes,old_controller)end

[8]ページ先頭

©2009-2025 Movatter.jp