2012-01-17 145 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