Do routing specs support redirect routes? [RSpec]

牧云@^-^@ 提交于 2019-12-20 11:06:05

问题


After digging fairly deeply on this issue, I've come to an impasse between my understanding of the documentation and my results.

According to https://www.relishapp.com/rspec/rspec-rails/v/2-8/docs/routing-specs/route-to-matcher, we should be able to write the following:

#rspec-rails (2.8.1)
#rspec (>= 1.3.1)
#rspec-core (~> 2.8.0)

# routing spec
require "spec_helper"

describe BusinessUsersController do
  describe "routing" do
    it "routes to some external url" do
      get("/business_users/7/external_url").should route_to("http://www.google.com")
    end
  end
end

# routes.rb
BizeebeeBilling::Application.routes.draw do

  resources :business_users do
    member do
      get "external_url" => redirect("http://www.google.com")
    end
  end
end

Running this spec produces the following results: Failures:

  1) BusinessUsersController routing routes to some external url
     Failure/Error: assert_routing "/business_users/7/external_url", "http://www.google.com"
     ActionController::RoutingError:
       No route matches "/business_users/7/external_url"
     # ./spec/routing/business_users_routing_spec.rb:19:in `block (3 levels) in <top (required)>'

I have not been able to find anyone reporting this specific issue anywhere.

Added detail: the route is resolved perfectly well when testing manually.


回答1:


Routing specs/tests specialize in testing whether a route maps to a specific controller and action (and maybe some parameters too).

I dug into the internals of Rails and Journey a bit. RSpec and Rails (basically, some details left out) use Rails.application.routes.recognize_path to answer the question "is this routable?"

For example:

$ rails console
> Rails.application.routes.recognize_path("/business_users/1", method: "GET")
 => {:action=>"show", :controller=>"business_users", :id=>"1"}

However, there's no controller on the other end of /business_users/1/external_url. In fact, to perform the redirect, Rails has created an instance of ActionDispatch::Routing::Redirect, which is a small Rack application. No Rails controller is ever touched. You're basically mounting another Rack application to perform the redirection.

To test the redirect, I recommend using a request spec instead (a file in spec/requests). Something like:

require "spec_helper"

describe "external redirection" do
  it "redirects to google.com" do
    get "/business_users/1/external_url"
    response.should redirect_to("http://www.google.com")
  end
end

This tests the route implicitly, and allows you to test against the redirection.




回答2:


Andy Lindeman has the correct answer. However, you don't have to put the spec in spec/requests, you can keep it in spec/routing and be explicit with the metadata "type": describe 'my route', type: :request do




回答3:


I was running into a similar case where I was trying to test a series of routes, some which should redirect and some which shouldn't. I wanted to keep them in a single routing spec, since that was the most logical way to group them.

I tried using describe: 'my route', type: request, but found that not to work. However, you can include RSpec::Rails::RequestExampleGroup in your spec context to gain access to the request spec methods. Something like:

describe "My Routes" do
  context "Don't Redirect" do
    it "gets URL that doesn't redirect" do
      get("business_users/internal_url").should route_to(controller: "business_users", action: "internal_url_action")
    end
  end

  context "Redirection" do
    include RSpec::Rails::RequestExampleGroup
    it "redirects to google.com" do
      get "/business_users/1/external_url"
      response.should redirect_to("http://www.google.com")
    end
  end
end



回答4:


The simplest way to test external redirects is to use an integration test:

  test "GET /my_page redirects Google" do
    get "/my_page"
    assert_redirected_to "https://google.com"
  end

You test needs to be under your test/integration directory or the equivalent directory where the integration tests should go.




回答5:


I think you want the redirect_to matcher.



来源:https://stackoverflow.com/questions/10842448/do-routing-specs-support-redirect-routes-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!