我有一个健康端点,它将检查数据库连接是否正常工作:
class HealthController < ApplicationController
def health
User.any? # Force a DB connection to see if the database is healthy
head :ok
rescue StandardError
service_unavailable # Defined in ApplicationController
end
end
我想在数据库连接失败时测试 503 状态,但我不确定如何在 RSpec 中模拟数据库失败:
require 'swagger_helper'
RSpec.describe 'Health' do
path '/health' do
get 'Returns API health status' do
security []
response '200', 'API is healthy' do
run_test!
end
response '503', 'API is currently unavailable' do
# Test setup to mock database failure goes here
run_test!
end
end
end
end