RUBY-RSPEC名称错误:你是说?CategorySerializer



早上好。我想了解为什么在测试我的RSPEC(类别控制器(时,它会给出以下错误消息:

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

这是我的Factorybot

FactoryBot.define dofactory:类别do序列(:name({|n|"类别#{n}"}终止结束

这是我的规范/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

这是我的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

在我看来,您似乎忘记添加此类的所有模块。尝试更新spec/controllers/api/v1/categories_controller_spec.rb:


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

相关内容

  • 没有找到相关文章

最新更新