【问题标题】:Rspec fails because too few arguments when rescuing errorRspec 失败,因为救援错误时参数太少
【发布时间】:2019-05-06 19:08:55
【问题描述】:

在系统规范中,我正在尝试测试对数据库超时的正确处理。发生这种情况时,会引发一个新的 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:in `format' ....

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

 ArgumentError:
   too few arguments

我在这里遗漏了什么吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多