我正在尝试编写一个控制器测试,测试子域约束。但是,我无法让RSpec设置子域并在子域不准确时返回错误。
我正在使用Rails 4.2.6和RSpec ~3.4
routes.rb
namespace :frontend_api do
constraints subdomain: 'frontend-api' do
resources :events, only: [:index]
end
end
events_controller.rb
module FrontendAPI
class EventsController < FrontendAPI::BaseController
def index
render json: []
end
end
end
规格
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
还有其他的方法吗?