我正在使用 Rails 構建 JSON 后端 API,但我似乎無法弄清楚:如何使用 RSpec 測驗 Rails 默認錯誤回應?例如,對于一個通用的 404 請求,我目前有以下請求規范:
require 'rails_helper'
RSpec.describe '404 errors' do
let(:json) { JSON.parse(response.body) }
before { get '/noexistent', headers: headers }
# examples
end
然而,Rails 拒絕呈現它通常會產生的 404 回應,而是簡單地引發:
ActionController::RoutingError:
沒有路由匹配 [GET] "/404"
我試圖消除config.consider_all_request_local = true從config/environments/test.rb,但沒有任何結果。
請注意,我已經看到了這個問題(這是我的想法consider_all_requests_local),但它適用于開發,而不是測驗。
編輯:我config.exceptions_app = self.routes用來將錯誤重定向到ErrorsController. 這在開發或生產中可以正常作業,但在測驗時卻不行。在測驗時,我仍然收到上述錯誤。有任何想法嗎?
uj5u.com熱心網友回復:
事實證明,答案相當簡單。我不得不改變這一行config/environments/test.rb:
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
到:
# Do not raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = true
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/406649.html
標籤:
上一篇:RubyERB渲染和def函式
