2013-01-21 106 views
0

剛剛偶然發現了這個問題,並在寫問題時找到了答案 - 因爲這是一個常見的用例,我想我會分享它。直到一分鐘前,我有以下問題:用rspec自定義404錯誤路由

我已經建立了一個自定義404頁使用錯誤控制器用下面的代碼我的Rails應用程序:

def index 
    render "errors/index", status: 404 
end 

我已經把我的路線呈現這個404頁面只要訪問不存在的路由:

devise_for :users 

get "errors/index", as: :error 
get "*not_found", to: "errors#index" # this is the important line 

root to: "welcome#index" 

而事實上,它確實有效。出於某種原因,但是,我對這個天賦不起作用:

it "renders 404 on unknown route" do 
    get("/thisrouteiswrong").should route_to("errors#index") 
end 

在終端生成的輸出是:

The recognized options <{"controller"=>"errors", "action"=>"index", "not_found"=>"thisrouteiswrong"}> did not match <{"controller"=>"errors", "action"=>"index"}>, difference: <{"not_found"=>"thisrouteiswrong"}>. 
    <{"controller"=>"errors", "action"=>"index"}> expected but was 
    <{"controller"=>"errors", "action"=>"index", "not_found"=>"thisrouteiswrong"}>. 

回答

3

隨着終端的錯誤顯示,要求有一個所謂的多個參數通過路線設置的not_found。我所要做的就是將這個參數傳遞給測試:

it "renders 404 on unknown route" do 
    get("/thisrouteiswrong").should route_to("errors#index", "not_found" => "thisrouteiswrong") 
end