Good Morning. I wanted to understand why when testing my RSPEC, the Categories controller, it is giving the error message below:
An error occurred while loading ./spec/controllers/api/v1/categories_controller_spec.rb.
Failure/Error:
RSpec.describe CategoriesController, type => :controller do
describe "GET index" do
it 'has a 200 status code' do
get :index
expect(response.status).to eq(200)
end
end
end
NameError:
uninitialized constant CategoriesController
Did you mean? CategorySerializer
# ./spec/controllers/api/v1/categories_controller_spec.rb:3:in `<top (required)>'
# /usr/local/bundle/gems/bootsnap-1.10.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:68:in `load'
# /usr/local/bundle/gems/bootsnap-1.10.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:68:in `load'
No examples found.
Finished in 0.00004 seconds (files took 0.89986 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
This is my Factorybot
FactoryBot.define do
factory :category do
sequence(:name) { |n| "Category #{n}" }
end
end
This is my spec/controllers/api/v1/categories_controller_spec.rb
require 'rails_helper'
RSpec.describe CategoriesController, type => :controller do
describe "GET index" do
it 'has a 200 status code' do
get :index
expect(response.status).to eq(200)
end
end
end
This is my rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'simplecov'
SimpleCov.start do
add_group 'Config', 'config'
add_group 'Controllers', 'app/controllers'
add_group 'Libs', 'lib'
add_group 'Models', 'app/models'
add_group 'Serializers', 'app/serializers'
add_group 'Specs', 'spec'
end
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => and
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
config.include Request::JsonHelpers, type: :request
end
module Api
module V1
class CategoriesController < ApplicationController
include ErrorSerializer
before_action :set_category, only: [:show, :update, :destroy]
# GET /categories
def index
@categories = Category.order("created_at DESC").page(params[:page].try(:[], :number))
render json: @categories
end
# GET /categories/1
def show
render json: @category
end
# POST /categories
def create
@category = Category.new(category_params)
@category.rental_company = RentalCompany.find(params[:rental_company_id])
if @category.save
render json: @category, status: :created
else
render json: ErrorSerializer.serialize(@category.errors), status: :unprocessable_entity
end
end
# PATCH/PUT /categories/1
def update
if @category.update(category_params)
render json: @category
else
render json: ErrorSerializer.serialize(@category.errors), status: :unprocessable_entity
end
end
# DELETE /categories/1
def destroy
@category.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_category
@category = Category.find(params[:id])
end
# Only allow a list of trusted parameters through.
def category_params
params.require(:category).permit(:id, :name, :description, :rental_company_id)
end
end
end
end
>Solution :
It seems to me that you forgot to add all the modules of this class. Try to update the spec/controllers/api/v1/categories_controller_spec.rb
to:
require 'rails_helper'
RSpec.describe Api::V1::CategoriesController, type => :controller do
describe "GET index" do
it 'has a 200 status code' do
get :index
expect(response.status).to eq(200)
end
end
end