【问题标题】:How to test before action of ApplicationController in rails using RSpec如何在使用 RSpec 的 Rails 中测试 ApplicationController 的操作之前
【发布时间】:2019-01-09 07:16:03
【问题描述】:

我的 ApplicationController 看起来像这样

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

我的控制器规格之一看起来像这样

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

现在的问题是,当我运行此规范时,我收到以下错误:

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'

现在我知道问题出在应用程序控制器中,但我不知道如何解决。我刚开始学习测试,有人可以帮助我吗?谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby rspec factory-bot rspec-rails


    【解决方案1】:

    在发送请求之前使用FactoryBot 创建Company 记录。

    在你的 spec/factories/companies.rb 中:

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

    在您的规范/控制器/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
    

    所以现在,在您的initialize_fields 中,您将在数据库中找到公司记录,并消除错误。

    【讨论】:

      猜你喜欢
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多