routing_test.rb 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. require 'test_helper'
  18. require './config/routes.rb'
  19. class RoutingTest < ActionController::TestCase
  20. def test_recognizes
  21. ActionController::Routing.use_controllers! ["store"]
  22. load "./config/routes.rb"
  23. # Check the default index action gets generated
  24. assert_recognizes({"controller" => "store", "action" => "index"}, "/store")
  25. # Check routing to an action
  26. assert_recognizes({"controller" => "store", "action" => "list"},
  27. "/store/list")
  28. # And routing with a parameter
  29. assert_recognizes({ "controller" => "store",
  30. "action" => "add_to_cart",
  31. "id" => "1" },
  32. "/store/add_to_cart/1")
  33. # And routing with a parameter
  34. assert_recognizes({ "controller" => "store",
  35. "action" => "add_to_cart",
  36. "id" => "1",
  37. "name" => "dave" },
  38. "/store/add_to_cart/1",
  39. { "name" => "dave" } ) # like having ?name=dave after the URL
  40. # Make it a post request
  41. assert_recognizes({ "controller" => "store",
  42. "action" => "add_to_cart",
  43. "id" => "1" },
  44. { :path => "/store/add_to_cart/1", :method => :post })
  45. end
  46. def test_generates
  47. ActionController::Routing.use_controllers! ["store"]
  48. load "config/routes.rb"
  49. assert_generates("/store", :controller => "store", :action => "index")
  50. assert_generates("/store/list", :controller => "store", :action => "list")
  51. assert_generates("/store/add_to_cart/1",
  52. { :controller => "store", :action => "add_to_cart",
  53. :id => "1", :name => "dave" },
  54. {}, { :name => "dave"})
  55. end
  56. def test_routing
  57. ActionController::Routing.use_controllers! ["store"]
  58. load "config/routes.rb"
  59. assert_routing("/store", :controller => "store", :action => "index")
  60. assert_routing("/store/list", :controller => "store", :action => "list")
  61. assert_routing("/store/add_to_cart/1",
  62. :controller => "store", :action => "add_to_cart", :id => "1")
  63. end
  64. def test_alternate_routing
  65. ActionController::Routing.use_controllers! ["store"]
  66. load "config/routes.rb"
  67. assert_generates("/store", :controller => "store")
  68. with_routing do |set|
  69. set.draw do |map|
  70. map.connect "shop/:action/:id", :controller => "store"
  71. assert_generates("/shop", :controller => "store")
  72. assert_recognizes({:controller => "store", :action => "index"}, "/shop")
  73. end
  74. end
  75. assert_generates("/store", :controller => "store")
  76. end
  77. end