2016-06-29 11 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を〜のRails 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

これは、beforeブロックでホストを設定する代わりに、テストで完全な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

ここに答えます
関連する問題