0

I'm writing a Rails API and have namespaced controllers (and routes). In trying to write RSpec tests for said controllers, I'm getting errors and am not certain how to rectify my mistakes. The controller is as follows:

module Api
  module V1
    class BudgetsController < ApplicationController
      before_action :authenticate_user!
      before_action :set_budget, only: %i[destroy]

      def new
        @budget = current_user.budget.build
        render json: @budget
      end

      def create
        @budget = current_user.budget.build(budget_params)
        if @budget.save
          render json: @budget, status: 200
        else
          render json: { errors: @budget.errors.full_messages }, status: 422
        end
      end

      def destroy
        @budget.destroy
      end

      private

      def set_budget
        @budget = Budget.find(params[:id])
      end

      def budget_params
        params.require(budget).permit(:start_date, :end_date, :income)
      end
    end
  end
end

The test (so far) is as follows:

require 'spec_helper'

RSpec.describe BudgetsController, type: :controller do
  let(:valid_attributes) do
    {
      start_date: "01-03-2020",
      end_date: "31-03-2020",
      income: 7000
    }
  end

  let(:invalid_attributes) do
    {
      start_date: nil,
      end_date: nil,
      income: nil
    }
  end

  describe "GET #index" do
    it "returns a success response" do
      get :index, params: {}
      expect(response).to be_successful
    end
  end
end

And just for reference, the routes are set up as follows:

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      devise_for :users
      resources :users, only: %i[show index]
      resources :budgets
      resources :budget_totals
    end
  end
end

The error I'm getting is as follows:

Failure/Error:
  RSpec.describe BudgetsController, type: :controller do
    let(:valid_attributes) do
      {
        start_date: "01-03-2020",
        end_date: "31-03-2020",
        income: 7000
      }
    end

    let(:invalid_attributes) do

NameError:
  uninitialized constant BudgetsController
# ./spec/controllers/budgets_controller_spec.rb:3:in `<top (required)>'
No examples found.


Finished in 0.00008 seconds (files took 8.51 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

PS. The controllers are in the app/controllers/api/v1 folder.

Genetic1989
  • 614
  • 1
  • 6
  • 19
  • Your situation maybe similar to this https://stackoverflow.com/questions/29920216/ruby-namespacing/29920332#29920332 – Paa Yaw Mar 01 '20 at 23:41

1 Answers1

0

Use namespace while describing the controller, do this

RSpec.describe Api::V1::BudgetsController, type: :controller do

Suggestion

You should better cover APIs with Request specs as well

More info on request specs - https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

Give it a try!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • When I try this, the error changes to: ``` NameError: uninitialized constant Api # ./spec/controllers/budgets_controller_spec.rb:3:in `' ``` – Genetic1989 Mar 02 '20 at 01:14