2012-01-17 88 views
3

我有Rails和Rspec的路由問題,我無法解決。使用rspec測試路由重定向

在我的routes.rb文件我已經設置了一些重定向如下:

match "/events" => redirect("/albums") 
match "/events/:id" => redirect("/albums/%{id}") 

這工作,但我不能工作,如何對其進行測試。我希望我能做到像

{ :get => "/events" }.should route_to(:controller => "events", :action => "index") 

{ :get => "/events" }.should redirect_to("/albums") 

然而事情沒有這些選項似乎工作。

創建正常的請求響應週期,獲取「/事件」,然後檢查響應的是重定向返回一個錯誤,沒有反應......

回答

0

你試過 {:GET =>「/events「} .should route_to(:controller =>」albums「,:action =>」index「)?

+2

導致: 的ActionController :: RoutingError: 無路由匹配「/事件」 (即使試圖訪問/事件確實事實上重定向 – 2012-01-18 12:35:37

5

您可以在請求規格中對其進行測試(在spec/requests中)。這裏有一個例子:

require "spec_helper" 

describe "redirection" do 
    it "should redirect events to albums" do 
    get "/events/1" 
    response.should redirect_to("/albums/1") 
    end 
end