Agile Web Development with Rails, Edition 4
    
    
  
  
    Agile Web Development with Rails, Edition 4
    
      21.1 Views
      19 Active Record
    
    
      20.1 Testing Routes
    
    edit test/unit/routing_test.rb
    require 'test_helper'
    require './config/routes.rb'
     
    class RoutingTest < ActionController::TestCase
     
      def test_recognizes
        # Check the default index action gets generated
        assert_recognizes({"controller" => "store", "action" => "index"}, "/")
        
        # Check routing to an action
        assert_recognizes({"controller" => "products", "action" => "index"}, 
                          "/products")
        
        # And routing with a parameter
        assert_recognizes({ "controller" => "line_items", 
                            "action" => "create", 
                            "product_id" => "1" },
                            {path: "/line_items", method: :post},
                            {"product_id" => "1"})
      end
      
      def test_generates
        assert_generates("/", controller: "store", action: "index")
        assert_generates("/products", 
                         { controller: "products", action: "index"})
        assert_generates("/line_items", 
                         { controller: "line_items", action: "create", 
                           product_id: "1"},
                         {method: :post}, { product_id: "1"})
      end
      
      def test_routing
        assert_routing("/", controller: "store", action: "index")
        assert_routing("/products", controller: "products", action: "index")
        assert_routing({path: "/line_items", method: :post},
                         { controller: "line_items", action: "create", 
                           product_id: "1"},
                         {}, { product_id: "1"})
      end
        
    end
    rake test:units
    Run options: --seed 56030
     
    # Running tests:
     
    ..........
     
    Finished tests in 1.084821s, 9.2181 tests/s, 42.4033 assertions/s.
     
    10 tests, 46 assertions, 0 failures, 0 errors, 0 skips
    
      21.1 Views
      19 Active Record