Я пишу Rails API и имею контроллеры пространства имен (и маршруты). Пытаясь написать тесты RSpe c для указанных контроллеров, я получаю ошибки и не уверен, как их исправить. Контроллер выглядит следующим образом:
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
Тест (пока) выглядит следующим образом:
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
И просто для справки, маршруты настроены следующим образом:
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
Я получаю следующую ошибку:
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. Контроллеры находятся в папке app / controllers / api / v1.