用RSPEC测试路由
基本
require "spec_helper"
describe "routes for Widgets" do
it "routes /widgets to the widgets controller" do
{ :get => "/widgets" }.
should route_to(:controller => "widgets", :action => "index")
end
end
{ :delete => "/accounts/37" }.should_not be_routable
require "spec_helper"
describe "routes for Widgets" do
it "routes /widgets to the widgets controller" do
get("/widgets").
should route_to("widgets#index")
end
end
#路由如下
match '/:city/*destination' => 'cities#myaction', :constraints => {:city => /#{City.all.map{|c|c.slug}.join('|')}/}
describe "routing" do
before(:each) do
@mock_city = mock_model(City, :id => 42, :slug => 'some-city')
City.stub!(:find_by_slug => @mock_city, :all => [@mock_city])
MyApp::Application.reload_routes!
end
it "recognizes and generates a route for city specific paths" do
{ :get => '/some-city/some/path' }.should route_to({:controller => "cities", :action => "myaction", :destination => 'some/path', :city => 'some-city'})
end
it "rejects city paths for cities that don't exist in the DB" do
{ :get => '/some-bad-city/some/path' }.should_not be_routable
end
end
good.