routing_test.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. require 'test_helper'
  10. require './config/routes.rb'
  11. class RoutingTest < ActionController::TestCase
  12. def test_recognizes
  13. # Check the default index action gets generated
  14. assert_recognizes({"controller" => "store", "action" => "index"}, "/")
  15. # Check routing to an action
  16. assert_recognizes({"controller" => "products", "action" => "index"},
  17. "/products")
  18. # And routing with a parameter
  19. assert_recognizes({ "controller" => "line_items",
  20. "action" => "create",
  21. "product_id" => "1" },
  22. {path: "/line_items", method: :post},
  23. {"product_id" => "1"})
  24. end
  25. def test_generates
  26. assert_generates("/", controller: "store", action: "index")
  27. assert_generates("/products",
  28. { controller: "products", action: "index"})
  29. assert_generates("/line_items",
  30. { controller: "line_items", action: "create",
  31. product_id: "1"},
  32. {method: :post}, { product_id: "1"})
  33. end
  34. def test_routing
  35. assert_routing("/", controller: "store", action: "index")
  36. assert_routing("/products", controller: "products", action: "index")
  37. assert_routing({path: "/line_items", method: :post},
  38. { controller: "line_items", action: "create",
  39. product_id: "1"},
  40. {}, { product_id: "1"})
  41. end
  42. end