From c27b9db39fd1d86448572c06df2bd097debbfe10 Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Wed, 5 Dec 2007 18:17:23 +0000 Subject: Add many examples to assertion documentation. Closes #7803 [jeremymcanally] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8300 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../assertions/routing_assertions.rb | 68 ++++++++++++++++++---- 1 file changed, 56 insertions(+), 12 deletions(-) (limited to 'actionpack/lib/action_controller/assertions/routing_assertions.rb') diff --git a/actionpack/lib/action_controller/assertions/routing_assertions.rb b/actionpack/lib/action_controller/assertions/routing_assertions.rb index 6cd4d48b9e..9bff283245 100644 --- a/actionpack/lib/action_controller/assertions/routing_assertions.rb +++ b/actionpack/lib/action_controller/assertions/routing_assertions.rb @@ -1,25 +1,41 @@ module ActionController module Assertions + # Suite of assertions to test routes generated by Rails and the handling of requests made to them. module RoutingAssertions - # Asserts that the routing of the given path was handled correctly and that the parsed options match. + # Asserts that the routing of the given +path+ was handled correctly and that the parsed options (given in the +expected_options+ hash) + # match +path+. Basically, it asserts that Rails recognizes the route given by +expected_options+. # - # assert_recognizes({:controller => 'items', :action => 'index'}, 'items') # check the default action - # assert_recognizes({:controller => 'items', :action => 'list'}, 'items/list') # check a specific action - # assert_recognizes({:controller => 'items', :action => 'list', :id => '1'}, 'items/list/1') # check an action with a parameter - # - # Pass a hash in the second argument to specify the request method. This is useful for routes + # 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. # # # assert that POSTing to /items will call the create action on ItemsController # assert_recognizes({:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post}) # - # You can also pass in "extras" with a hash containing URL parameters that would normally be in the query string. This can be used + # You can also pass in +extras+ 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: # # # 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" }) + # + # The +message+ parameter allows you to pass in an error message that is displayed upon failure. + # + # ==== Examples + # # 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') def assert_recognizes(expected_options, path, extras={}, message=nil) if path.is_a? Hash request_method = path[:method] @@ -43,12 +59,24 @@ module ActionController end end - # Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. - # For example: + # Asserts that the provided options can be used to generate the provided path. This is the inverse of #assert_recognizes. + # The +extras+ parameter is used to tell the request the names and values of additional request parameters that would be in + # a query string. The +message+ parameter allows you to specify a custom error message for assertion failures. # + # The +defaults+ parameter is unused. + # + # ==== Examples + # # 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" } def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil) clean_backtrace do expected_path = "/#{expected_path}" unless expected_path[0] == ?/ @@ -67,9 +95,25 @@ module ActionController end end - # Asserts that path and options match both ways; in other words, the URL generated from - # options is the same as path, and also that the options recognized from path are the same as options. This - # essentially combines assert_recognizes and assert_generates into one step. + # Asserts that path and options match both ways; in other words, it verifies that path generates + # options and then that options generates path. This essentially combines #assert_recognizes + # and #assert_generates into one step. + # + # The +extras+ hash allows you to specify options that would normally be provided as a query string to the action. The + # +message+ parameter allows you to specify a custom error message to display upon failure. + # + # ==== Examples + # # 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"} def assert_routing(path, options, defaults={}, extras={}, message=nil) assert_recognizes(options, path, extras, message) -- cgit v1.2.3