2013-11-01 33 views
0

儘管能夠通過瀏覽器成功加載,但我仍然收到錯誤的測試控制器路由。 Rails4 + rspec。儘管路由器在瀏覽器中加載OK,但自定義Rails路由測試失敗

任何想法?

#controller spec 
describe PublicSitesController do 

    describe "GET index" do 
    it "returns success" do 
     get :index      #line 7 in the spec file 
     response.status.should == 200 
    end 
    end 

end 


#routes 
get ":site_name/:page_name", to: "public_sites#show" 
get ":site_name", to: 'public_sites#index' 
get "/", to: 'public_sites#root' 


#controller 
class PublicSitesController < ApplicationController 

    def root 
    end 

    def index 
    end 

    def show 
    end 

end 

#the error: 
Failures: 

1) PublicSitesController GET index returns success 
    Failure/Error: get :index 
    ActionController::UrlGenerationError: 
    No route matches {:action=>"index", :controller=>"public_sites"} 
    # ./spec/controllers/public_sites_controller_spec.rb:7:in `block (3 levels) in <top (required)>' 

以下是通過耙路線相關的路線:

 GET /:site_name/:page_name(.:format)     public_sites#show 
     POST /:site_name/:page_name(.:format)     public_sites#receive_form 
     GET /:site_name(.:format)        public_sites#index 
     GET /            public_sites#root 
+0

複製「耙路線」的輸出稱​​它爲 – arieljuod

+0

添加的耙路輸出的要求 – aaandre

+0

也許 :M調用控制器的動作

編輯2嘗試'get'/ public_sites/index''來查看路由中的site_name符號是否可以被「解析」爲測試範圍內的值 – Iralution

回答

1

你缺少對請求一些參數,路由器不知道該怎麼做「:SITE_NAME」,你可以試試:

get :index, site_name: 'something' 

編輯:

,當你調用GET/POST /等測試中調用操作名稱的Wi日該方法,而不是URL,這樣控制器測試,使這一行動工作URL的獨立的(你可以改變的URL和控制仍然有效)

你的路線告訴它需要命名的一些參數軌「site_name」,所以你需要告訴rails什麼是「site_name」裏面的一個動作參數

如果你想讓你有路由測試,參數https://www.relishapp.com/rspec/rspec-rails/v/2-3/docs/routing-specs

當您在瀏覽器上打開網站時,您並未調用該操作,您實際上正在運行整個應用程序,然後是路由系統如果你想測試的表演動作,你應該

get :show, site_name: 'some_site', page_name: 'some_page' 
+0

謝謝,這使測試通過了。然而,'get'/ the-site-name''會導致'無路由匹配{:controller =>「public_sites」,:action =>「/ site-name/the-page-name」}。而瀏覽器測試顯示正確的視圖。你能詳細解釋一下嗎? – aaandre

+0

好的,在答案上加了解釋 – arieljuod

+0

謝謝。我已經通過路由測試'{:get =>「/ the-site-name/the-page-name"}.should route_to(」public_sites#show「,site_name:」the-site-name「,page_name:」the ),這部分是爲什麼這種情況如此混亂。 – aaandre

相關問題