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
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
.............
Finished in 0.352850 seconds.
 
13 tests, 49 assertions, 0 failures, 0 errors, 0 skips
 
Test run options: --seed 54863

21.1 Views 19 Action Controller