Agile Web Development with Rails, Edition 4

21.1 Views 19 Action Controller

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: 
 
# Running tests:
 
..........
 
Finished tests in 0.224844s, 44.4753 tests/s, 204.5865 assertions/s.
 
10 tests, 46 assertions, 0 failures, 0 errors, 0 skips
 
ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]

21.1 Views 19 Action Controller