ruby-on-rails - Rspec GET 到不同的端点一秒钟 - ActionController::UrlGenerationError
问题描述
在我的 Grape/Rails 应用程序中,我实现了维护模式,ApplicationController
因此当此模式处于活动状态时,它将重定向到maintenance_mode_path
应用程序中的任何位置。如何强制 rspec 暂时处于不同的端点,例如。api/v1/new_endpoint
而整个测试发生在MaintenanceModeController
?
maintenance_mode_controller_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 请求,因此它无法与系统中的其他控制器进行实际交互。
请改用请求规范:
# /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
请求规范提供了控制器规范的高级替代方案。事实上,从 RSpec 3.5 开始,Rails 和 RSpec 团队都不鼓励直接测试控制器,而是支持像请求规范这样的功能测试。
推荐阅读
- lua - Lua:如何使用 pcall 查看循环是否崩溃
- python - 将一维 pandas 数据帧转换为 N 维 numpy 数组
- python - 在python中交换两行
- android - getActivity() 在 Fragment 中变为 null
- python - 我如何计算python中每个质心的密度?
- django - Django:复选框 Onclick 事件
- node.js - 如何等待带有嵌套 foreach 循环的 Async/Await 完成
- nim-lang - 如何从 nim 表中获取可修改的值
- c++ - u 后缀和无符号修饰符之间的真正区别是什么?
- python - Python - 以类似格式抓取数据