Rspec失败,因为拯救错误时参数太少

Rspec fails because too few arguments when rescuing error

在系统规范中,我正在尝试测试对数据库超时的正确处理。当发生这种情况时,将引发一个新的 TinyTds::Error

这里是我的控制器(EMData 处理数据库连接)

class Json::ChartController < ApplicationController
  rescue_from TinyTds::Error, with: :connection_timed_out

  def index
    data = EMData.call(params) 

    respond_to do |format|
      format.json { render json: data }
    end
  end


  def connection_timed_out(_error)
    format.json { head :request_timeout }
  end
end

这是我的规格

context 'when the SQL Server connection times out' do
  let(:data_class) { class_spy('EMData').as_stubbed_const }

    it 'a feedback message is displayed' do
      allow(data_class).to receive(:call).and_raise(TinyTds::Error.new('message'))
      ...
      SUBMIT FORM VIA JS
      ...
      expect(page).to have_content("Some Content")
      end

规范对我来说似乎很简单。但是,当我 运行 它时,我得到

Rack app error handling request { GET /json/chart/ }

/app/controllers/json/chart_controller.rb:24:in `format' ....

Failure/Error: format.json { head :request_timeout }

 ArgumentError:
   too few arguments

我是不是漏了什么?

您在 connection_timed_out(_error) 中缺少 respond_to do |format|。它应该是这样的:

def connection_timed_out(_error)
  respond_to do |format|
    format.json { head :request_timeout }
  end
end