Search code examples
ruby-on-railsrspectiny-tds

Rspec fails because too few arguments when rescuing error


In a system spec, I'm trying to test the correct handling of a database timeout. When that happens a new TinyTds::Error is raised.

Here my controller (EMData handles the DB connection)

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

Here my spec

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

The spec seems pretty straightforward to me. However, when I run it , I get

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

Am I misisng anything here?


Solution

  • You're missing the respond_to do |format| in connection_timed_out(_error). It should be like:

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