我试图为我的实体控制器编写rspec测试,但它给出了错误,这里我没有使用Factory bot,错误是:
Failures:
1) EntitiesController Get index checks for the status code
Failure/Error: @entities = @schema.entities
NoMethodError:
undefined method `entities' for nil:NilClass
# ./app/controllers/entities_controller.rb:13:in `index'
# ./spec/controllers/entities_controller_spec.rb:19:in `block (3 levels) in <top (required)>'
2) EntitiesController New checks for the status code
Failure/Error: @schema = Schema.find(params[:schema_id])
ActiveRecord::RecordNotFound:
Couldn't find Schema without an ID
# ./app/controllers/entities_controller.rb:19:in `new'
3) EntitiesController Entity create redirects to Entity page
Failure/Error: post :create, params: params
ActionController::UrlGenerationError:
No route matches {:action=>"create", :controller=>"entities", :entity=>{:clientId=>"1", :name=>"test", :description=>"test", :mnemonic=>"test", :schema_id=>1}}
4) EntitiesController Entity create redirects to Entity page
Failure/Error: post :create, params: params
ActionController::UrlGenerationError:
No route matches {:action=>"create", :controller=>"entities", :entity=>{:name=>"test1"}}
Finished in 1.23 seconds (files took 6.23 seconds to load)
16 examples, 4 failures
Failed examples:
rspec ./spec/controllers/entities_controller_spec.rb:18
rspec ./spec/controllers/entities_controller_spec.rb:26
rspec ./spec/controllers/entities_controller_spec.rb:34
rspec ./spec/controllers/entities_controller_spec.rb:40
我的实体控制器
class EntitiesController < ApplicationController
def index
if params[:search]
@schema = Schema.find_by(id: params[:schema_id])
@entities = @schema.entities.search(params[:search]).order("created_at DESC")
elsif params[:schema_id]
@schema = Schema.find_by(id: params[:schema_id])
@entities = @schema.entities
else
@schema = Schema.find_by(id: params[:id])
@entities = @schema.entities
end
end
def new
@schema = Schema.find(params[:schema_id])
@entity = Entity.new
end
def create
@schema = Schema.find(params[:schema_id])
@entity = @schema.entities.new(entity_params)
if @entity.save
redirect_to schema_entities_path(@schema)
else
redirect_to new_schema_entity_path(@schema)
end
end
private
def entity_params
params.require(:entity).permit(:clientId, :name, :description, :mnemonic)
end
end
路线。rb:
Rails.application.routes.draw do
get 'joins/index'
get 'fields/index'
get 'entities/index', to:'entities#index'
root "schemas#index"
resources :schemas do
resources :entities do
resources :fields do
resources :joins
end
end
end
end
这是我尝试的entitiesController的rspec:
# frozen_string_literal: true
require 'rails_helper'
require 'spec_helper'
RSpec.describe EntitiesController, type: :controller do
before(:each) do
@schema = Schema.create(name:'testEntity',schemaId:'',schemaMnemonic:'')
@schema = Schema.find_by(id:1)
# p @schema
@entity = Entity.create(clientId:'1',name:'test',description:'test',mnemonic:'test',schema_id:1)
# p @entity
#p @schema.entities
end
#For index
describe 'Get index' do
it 'checks for the status code' do
get :index
expect(response).to have_http_status(200)
end
end
#For new
describe 'New' do
it 'checks for the status code' do
get :new
expect(response).to have_http_status(200)
end
end
#For create
context 'Entity create' do
it 'redirects to Entity page' do
params = { entity: { clientId:'1',name:'test',description:'test',mnemonic:'test',schema_id:1 } }
post :create, params: params
expect(response).to have_http_status(302)
end
it 'redirects to Entity page' do
params = { entity: { name: 'test1' } }
post :create, params: params
expect(response).to have_http_status(302)
end
end
end
对于我的模式控制器,所有测试用例都正确通过,但在实体中,当我尝试编写rspec时,它显示的是@模式。实体为零,但当我写p'@模式时。显示正确的值。