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.