2016-06-29 8 views
4

하위 도메인 제약 조건을 테스트하는 컨트롤러 테스트를 작성하려고합니다. 그러나 RSpec에 하위 도메인을 설정하게 할 수 없으며 하위 도메인이 정확하지 않은 경우 오류를 반환합니다.RSpec & Rails 4를 사용하여 하위 도메인 제약 조건을 테스트하는 방법

나는 사양

module FrontendAPI 
    class EventsController < FrontendAPI::BaseController 
    def index 
     render json: [] 
    end 
    end 
end 

events_controller.rb

namespace :frontend_api do 
    constraints subdomain: 'frontend-api' do 
    resources :events, only: [:index] 
    end 
end 

3.4

routes.rb ~ 레일 4.2.6와 RSpec을 사용하고

RSpec.describe FrontendAPI::EventsController do 
    describe 'GET #index' do 
    context 'wrong subdomain' do 
     before do 
     @request.host = 'foo.example.com' 
     end 

     it 'responds with 404' do 
     get :index 
     expect(response).to have_http_status(:not_found) 
     end 
    end 
    end 
end 

다른 방법이 있습니까?

답변

2

이전 블록에서 호스트를 설정하는 대신 테스트에서 전체 URL을 사용하여이 작업을 수행 할 수 있습니다.

시도 :

RSpec.describe FrontendAPI::EventsController do 
    describe 'GET #index' do 
    let(:url) { 'http://subdomain.example.com' } 
    let(:bad_url) { 'http://foo.example.com' } 

    context 'wrong subdomain' do   
     it 'responds with 404' do 
     get "#{bad_url}/route" 
     expect(response).to have_http_status(:not_found) 
     end 
    end 
    end 
end 

비슷한 질문이 있습니다 및 testing routes with subdomain constraints using rspec

여기에 대답
관련 문제