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

时间:2019-05-06 10:43:32

标签: ruby-on-rails rspec tiny-tds

在系统规格中,我试图测试数据库超时的正确处理。发生这种情况时,会产生一个新的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

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

  

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

     

/app/controllers/json/chart_controller.rb:24:以“格式”...。

     

失败/错误:format.json {head:request_timeout}

 ArgumentError:
   too few arguments

我在这里误会吗?

1 个答案:

答案 0 :(得分:1)

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

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