Ruby on rails Rspec获取第二个的不同端点-ActionController::UrlGenerationError
在我的Grape/Rails应用程序中,我在Ruby on rails Rspec获取第二个的不同端点-ActionController::UrlGenerationError,ruby-on-rails,rspec,grape,Ruby On Rails,Rspec,Grape,在我的Grape/Rails应用程序中,我在ApplicationController中实现了维护模式,因此当此模式处于活动状态时,它将从应用程序中的任何位置重定向到维护模式路径。如何在整个测试在MaintenanceModelController中进行时,强制rspec在不同的端点上停留一段时间,例如api/v1/new_端点 maintenance\u mode\u controller\u spec context 'when maintenance mode is active' do
ApplicationController
中实现了维护模式,因此当此模式处于活动状态时,它将从应用程序中的任何位置重定向到维护模式路径。如何在整个测试在MaintenanceModelController
中进行时,强制rspec在不同的端点上停留一段时间,例如api/v1/new_端点
maintenance\u mode\u controller\u spec
context 'when maintenance mode is active' do
context 'when current page is not maintenance page' do
let(:call_endpoint) { get('/api/v1/new_endpoint') }
it 'redirect to the maintenance page' do
call_endpoint
expect(response).to have_http_status(:redirect)
end
end
end
但是上面的代码有一个错误
失败/错误:let(:call_endpoint){get('/api/v1/bank_partners')}
ActionController::UrlGenerationError:没有路由匹配{:action=>“/api/v1/new_endpoint”,:controller=>“maintenance_mode”}
您根本无法使用控制器规范来测试这一点。控制器规范使用模拟请求创建控制器实例,然后对其运行测试。例如,在控制器测试中调用get:show
时,实际上是在模拟控制器上调用#show
方法。
因为它实际上并不创建HTTP请求,所以它无法与系统中的其他控制器进行实际交互
请改为使用:
请求规范提供了控制器规范的高级替代方案。在里面
事实上,从RSPEC3.5开始,Rails和RSpec团队都不鼓励
直接测试控制器,支持功能测试,如请求
规格
# /spec/requests/maintainence_mode_spec.rb
require "rails_helper"
RSpec.describe "Maintenance mode", type: :request do
context 'when maintenance mode is active' do
context 'when current page is not maintenance page' do
let(:call_endpoint) { get('/api/v1/new_endpoint') }
it 'redirects to the maintenance page' do
call_endpoint
expect(response).to redirect_to('/somewhere')
end
end
end
end