Home > Software engineering >  Rspec for assosiate model
Rspec for assosiate model

Time:06-19

I tried to write rspec testing for my entity controller but it is giving error, Here I am not using Factory bot and the error is:

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'
     # ./spec/controllers/entities_controller_spec.rb:27:in `block (3 levels) in <top (required)>'

  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}}
     # ./spec/controllers/entities_controller_spec.rb:36:in `block (3 levels) in <top (required)>'

  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"}}
     # ./spec/controllers/entities_controller_spec.rb:42:in `block (3 levels) in <top (required)>'

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 # EntitiesController Get index checks for the status code
rspec ./spec/controllers/entities_controller_spec.rb:26 # EntitiesController New checks for the status code
rspec ./spec/controllers/entities_controller_spec.rb:34 # EntitiesController Entity create redirects to Entity page
rspec ./spec/controllers/entities_controller_spec.rb:40 # EntitiesController Entity create redirects to Entity page

My Entity Controller

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
  # binding.pry
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

Routes.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

This is rspec for my entitiesController that I tried:

# 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

For my Schema Controller all test cases are passing correctly but in entity when i try to write rspec it is showing @schema.entites are nil but when i write p '@schema.entites it is display correct value.

CodePudding user response:

As per your error @schema is nil, try to pass the params from your spec.

    describe 'Get index' do
      it 'checks for the status code' do
        get :index, params: { schema_id: 1 }
        expect(response).to have_http_status(200)
      end
    end
  • Related