<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActionController::Assertions::RoutingAssertions</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActionController::Assertions::RoutingAssertions</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../../files/lib/action_controller/assertions/routing_assertions_rb.html"> lib/action_controller/assertions/routing_assertions.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> Suite of assertions to test routes generated by Rails and the handling of requests made to them. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000215">assert_generates</a> <a href="#M000214">assert_recognizes</a> <a href="#M000216">assert_routing</a> </div> </div> </div> <!-- if includes --> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000215" class="method-detail"> <a name="M000215"></a> <div class="method-heading"> <a href="RoutingAssertions.src/M000215.html" target="Code" class="method-signature" onclick="popupCode('RoutingAssertions.src/M000215.html');return false;"> <span class="method-name">assert_generates</span><span class="method-args">(expected_path, options, defaults={}, extras = {}, message=nil)</span> </a> </div> <div class="method-description"> <p> Asserts that the provided options can be used to generate the provided path. This is the inverse of <tt><a href="RoutingAssertions.html#M000214">assert_recognizes</a></tt>. The <tt>extras</tt> parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The <tt>message</tt> parameter allows you to specify a custom error message for assertion failures. </p> <p> The <tt>defaults</tt> parameter is unused. </p> <h4>Examples</h4> <pre> # Asserts that the default action is generated for a route with no action assert_generates "/items", :controller => "items", :action => "index" # Tests that the list action is properly routed assert_generates "/items/list", :controller => "items", :action => "list" # Tests the generation of a route with a parameter assert_generates "/items/list/1", { :controller => "items", :action => "list", :id => "1" } # Asserts that the generated route gives us our custom route assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" } </pre> </div> </div> <div id="method-M000214" class="method-detail"> <a name="M000214"></a> <div class="method-heading"> <a href="RoutingAssertions.src/M000214.html" target="Code" class="method-signature" onclick="popupCode('RoutingAssertions.src/M000214.html');return false;"> <span class="method-name">assert_recognizes</span><span class="method-args">(expected_options, path, extras={}, message=nil)</span> </a> </div> <div class="method-description"> <p> Asserts that the routing of the given <tt>path</tt> was handled correctly and that the parsed options (given in the <tt>expected_options</tt> hash) match <tt>path</tt>. Basically, it asserts that Rails recognizes the route given by <tt>expected_options</tt>. </p> <p> Pass a hash in the second argument (<tt>path</tt>) 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. </p> <pre> # assert that POSTing to /items will call the create action on ItemsController assert_recognizes {:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post} </pre> <p> You can also pass in <tt>extras</tt> 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 string will end up in the params hash correctly. To test query strings you must use the extras argument, appending the query string on the path directly will not work. For example: </p> <pre> # assert that a path of '/items/list/1?view=print' returns the correct options assert_recognizes {:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" } </pre> <p> The <tt>message</tt> parameter allows you to pass in an error message that is displayed upon failure. </p> <h4>Examples</h4> <pre> # Check the default route (i.e., the index action) assert_recognizes {:controller => 'items', :action => 'index'}, 'items' # Test a specific action assert_recognizes {:controller => 'items', :action => 'list'}, 'items/list' # Test an action with a parameter assert_recognizes {:controller => 'items', :action => 'destroy', :id => '1'}, 'items/destroy/1' # Test a custom route assert_recognizes {:controller => 'items', :action => 'show', :id => '1'}, 'view/item1' # Check a Simply RESTful generated route assert_recognizes list_items_url, 'items/list' </pre> </div> </div> <div id="method-M000216" class="method-detail"> <a name="M000216"></a> <div class="method-heading"> <a href="RoutingAssertions.src/M000216.html" target="Code" class="method-signature" onclick="popupCode('RoutingAssertions.src/M000216.html');return false;"> <span class="method-name">assert_routing</span><span class="method-args">(path, options, defaults={}, extras={}, message=nil)</span> </a> </div> <div class="method-description"> <p> Asserts that path and options match both ways; in other words, it verifies that <tt>path</tt> generates <tt>options</tt> and then that <tt>options</tt> generates <tt>path</tt>. This essentially combines <tt><a href="RoutingAssertions.html#M000214">assert_recognizes</a></tt> and <tt><a href="RoutingAssertions.html#M000215">assert_generates</a></tt> into one step. </p> <p> The <tt>extras</tt> hash allows you to specify options that would normally be provided as a query string to the action. The <tt>message</tt> parameter allows you to specify a custom error message to display upon failure. </p> <h4>Examples</h4> <pre> # Assert 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 # Assert a basic route (controller + default action), with an error message if it fails assert_routing '/store', { :controller => 'store', :action => 'index' }, {}, {}, 'Route for store index not generated properly' # Tests a route, providing a defaults hash assert_routing 'controller/action/9', {:id => "9", :item => "square"}, {:controller => "controller", :action => "action"}, {}, {:item => "square"} # Tests a route with a HTTP method assert_routing { :method => 'put', :path => '/product/321' }, { :controller => "product", :action => "update", :id => "321" } </pre> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>