RSPEC失败了,因为在挽救错误时的争论太少



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

在这里我的控制器(EMData处理DB连接(

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

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

机架应用程序错误处理请求{get/json/chart/}

/app/controllers/json/chart_controller.rb:24:iin`

失败/错误:格式。

 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

最新更新