mardi 27 août 2019

Why got Rspec No route matches error with Rails?

Rails files

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # ...
end

app/controllers/api_application_controller.rb

class ApiApplicationController < ActionController::API
  # ...
end

app/controllers/iot_application_controller.rb

class IotApplicationController < ApiApplicationController
  # ...
end

app/controllers/iot/hardwares_controller.rb

class Iot::HardwaresController < IotApplicationController
  def index
    # ...
  end
end

RSpec file

spec/controllers/iot/hardwares_controller.rb

require 'rails_helper'

RSpec.describe Iot::HardwaresController, type: :controller do
  describe "GET #index" do
    context "with invalid params" do
      it "renders a JSON response with errors" do
        get :index, params: { "test": 1 }
        expect(response).to have_http_status(:bad_request)
      end
    end
  end
end

error

$ rspec .

Iot::HardwaresController
  GET #index
    with invalid params
      renders a JSON response with errors (FAILED - 1)

Failures:

  1) Iot::HardwaresController GET #index with invalid params renders a JSON response with errors
     Failure/Error: get :index, params: {"test": 1}

     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"iot/hardwares", :test=>1}
     # ./spec/controllers/iot/hardwares_controller.rb:17:in `block (4 levels) in <top (required)>'

Finished in 0.01547 seconds (files took 6.79 seconds to load)
1 example, 1 failure

How to write route in rspec correctly?

Aucun commentaire:

Enregistrer un commentaire