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
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
..........
Finished in 0.81034 seconds.
 
10 tests, 46 assertions, 0 failures, 0 errors

21.1 Views 19 Active Record