Search code examples
ruby-on-railsrubyrspecfactory-botrspec-rails

How to test before action of ApplicationController in rails using RSpec


My ApplicationController looks like this

class ApplicationController < ActionController::Base  
  before_action :initialize_fields

  protected

  def initialize_fields
    @company_id_super = nil
    @show_company_super = 0
    @round_id_super = nil
    if(params["controller"] == 'companies')
      if(params["id"].present?)
        @company_id_super = params["id"]
      end
    else
      if(params["company_id"].present?)
        @company_id_super = params["company_id"]
      end
    end
    if(@company_id_super != nil)
      @show_company_super = 1
      @company_super = Company.find(@company_id_super)
    else
      @company_super = nil
    end
    if(params["controller"] == 'home' || params[:controller] == 'votes')
      @hide_side_bar = 1
    else
      @hide_side_bar = 0
    end
    if(params["controller"] == 'rounds')
      if(params["id"].present?)
        @round_id_super = params["id"]
      end
    end
  end
end

and one of my controller specs looks like this

require 'rails_helper'

RSpec.describe OptionsController, type: :controller do
  describe 'Options controller request specs' do
    login_user
    context 'GET #index' do 
      it 'should success and render to index page' do
        contact = create(:option)
        get :index, params: { company_id: 1 }
        assigns(:options).should eq([option])
      end
    end

    context 'GET #show' do
      let!(:option) { create :option }
      it 'should success and render to edit page' do
        get :show, params: { id: option.id, company_id: 1 }
        expect(response).to render_template :edit
      end
    end
  end
end

Now the problem is when I run this spec I get the following error:

Failure/Error: @company_super = Company.find(@company_id_super)

ActiveRecord::RecordNotFound:
  Couldn't find Company with 'id'=1
# ./app/controllers/application_controller.rb:36:in `initialize_fields'

Now I know the problem is in the application controller but I do not know how to fix it. I just started learning tests, can anyone help me with it? Thanks !


Solution

  • Create the Company record using FactoryBot before sending the request.

    In your spec/factories/companies.rb:

    FactoryBot.define do
      factory :company do
        name 'My Company'
        .
        .
      end
    end
    

    In your spec/controllers/options_spec.rb

    RSpec.describe OptionsController, type: :controller do
    
      let(:my_company) { FactoryBot.create(:company) } 
    
      describe 'Options controller request specs' do
        login_user
          context 'GET #index' do 
            it 'should success and render to index page' do
               contact = create(:option)
               get :index, params: { company_id: my_company.id } #<--- This will create the company record and pass its ID
               assigns(:options).should eq([option])
            end
          end      
        end
      end
    end
    

    So now, in your initialize_fields, you will find the company record in the database and will rid you of the error.