简体   繁体   English

如何使用RSpec在Rails中测试ApplicationController的动作之前

[英]How to test before action of ApplicationController in rails using RSpec

My ApplicationController looks like this 我的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

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 ! 谢谢 !

Create the Company record using FactoryBot before sending the request. 发送请求之前,请使用FactoryBot创建Company记录。

In your spec/factories/companies.rb: 在您的规格/工厂/companies.rb中:

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

In your spec/controllers/options_spec.rb 在您的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. 因此,现在,在您的initialize_fields ,您将在数据库中找到公司记录,并将摆脱错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Minitest - 在Rails 4中测试ApplicationController before_action - Minitest - test ApplicationController before_action in Rails 4 Rails-使用rspec测试ApplicationController - Rails - Testing ApplicationController with rspec RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)? - RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)? Rails:如何通过控制器的before_action / after_action方法INLINE调用带有ARGS的ApplicationController方法? - Rails: How to call ApplicationController method WITH ARGS from a controller's before_action/after_action method INLINE? Rails / RSpec:如何使用第三方API测试Rails动作? - Rails / RSpec: How to test Rails action with third party API? Rails 3.1 Rspec Rails 2-如何测试此控制器操作? - Rails 3.1 Rspec Rails 2 - How can I test this controller action? Rails,RSpec:如何测试,特定的邮件程序操作被触发 - Rails, RSpec: How to test, that a specific mailer action gets triggered 如何在Rails控制器动作的RSpec测试中指定查询字符串? - How to specifiy the query string in an RSpec test of a Rails controller action? 如何使用Rails,Omniauth和Rspec对Facebook登录操作进行单元测试 - How to Unit Test Facebook Login Action with Rails, Omniauth and Rspec 如何使用RSpec在Rails 4中测试质量分配 - How to test Mass Assignment in Rails 4 using RSpec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM