【问题标题】:Testing routing redirects using rspec使用 rspec 测试路由重定向
【发布时间】:2012-02-12 16:13:30
【问题描述】:

我无法解决 Rails 和 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")

但是这些选项似乎都不起作用。

创建正常的请求响应周期,获取“/events”然后检查响应是重定向返回一个没有响应的错误...

【问题讨论】:

    标签: ruby-on-rails routing rspec


    【解决方案1】:

    您可以改为在请求规范中测试它(在规范/请求中)。这是一个例子:

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

    【讨论】:

      【解决方案2】:

      你试过了吗 { :get => "/events" }.should route_to(:controller => "albums", :action => "index") 吗?

      【讨论】:

      • 这会导致:ActionController::RoutingError: No route matches "/events"(即使尝试访问 /events 实际上会重定向
      猜你喜欢
      • 1970-01-01
      • 2011-08-21
      • 2011-08-04
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 2017-10-30
      • 2010-12-18
      相关资源
      最近更新 更多