2016-11-25 7 views
0

Я с Rails 5 API только приложение с помощью RSpec и версионируются таким образом:RSpec маршрутизации с поддомена

app 
    - controllers 
    - api 
     - v1 
     - users_controller.rb 

Мои api/v1/users_controller.rb:

module Api::V1 
    class UsersController < ApiController 

Мои config\routes.rb:

Rails.application.routes.draw do 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
    constraints subdomain: 'api' do 
    scope module: 'api' do 
     namespace :v1 do 
     resources :users 
     end 
    end 
    end 
end 

Когда я проверяю маршруты с rails routes, он показывает мне.

Prefix Verb URI Pattern    Controller#Action 
v1_users GET /v1/users(.:format)  api/v1/users#index {:subdomain=>"api"} 
     POST /v1/users(.:format)  api/v1/users#create {:subdomain=>"api"} 
v1_user GET /v1/users/:id(.:format) api/v1/users#show {:subdomain=>"api"} 
     PATCH /v1/users/:id(.:format) api/v1/users#update {:subdomain=>"api"} 
     PUT /v1/users/:id(.:format) api/v1/users#update {:subdomain=>"api"} 
     DELETE /v1/users/:id(.:format) api/v1/users#destroy {:subdomain=>"api"} 

Мой спецификации файла:

require "rails_helper" 

RSpec.describe Api::V1::UsersController, type: :routing do 
    describe "routing" do 

    it "routes to #index" do 
     expect(:get => "/v1/users").to route_to("api/v1/users#index") 
    end 

    it "routes to #create" do 
     expect(:post => "/v1/users").to route_to("api/v1/users#create") 
    end 

    it "routes to #show" do 
     expect(:get => "/v1/users/1").to route_to("api/v1/users#show", :id => "1") 
    end 

    it "routes to #update via PUT" do 
     expect(:put => "/v1/users/1").to route_to("api/v1/users#update", :id => "1") 
    end 

    it "routes to #update via PATCH" do 
     expect(:patch => "/v1/users/1").to route_to("api/v1/users#update", :id => "1") 
    end 

    it "routes to #destroy" do 
     expect(:delete => "/v1/users/1").to route_to("api/v1/users#destroy", :id => "1") 
    end 

    end 
end 

Но когда я тестирую свои маршруты с RSpec он не в ней.

bundle exec rspec spec/routing/users_routing_spec.rb 
FFFFF 

Failures: 

    1) Api::V1::UsersController routing routes to #index 
    Failure/Error: expect(:get => "/v1/users").to route_to("api/v1/users#index") 
     No route matches "/v1/users" 
    # ./spec/routing/users_routing_spec.rb:7:in `block (3 levels) in <top (required)>' 

Я не понимаю, почему. Есть идеи ?

ответ

0

Вы должны указать «поддомен» для своей спецификации.

before do 
    Rails.application.routes.default_url_options[:host] = 'test.host' 
end 

it "routes to #index" do 
    expect(:get => v1_users_url).to route_to('v1/users#index', subdomain: 'api') 
end