簡體   English   中英

Rspec - Stub/allow_any_instance_of 包含的模塊方法不起作用

[英]Rspec - Stub/allow_any_instance_of included module methods is not working

我一整天都在嘗試存根一個私有模塊方法,但沒有任何進展。

這是我的應用程序的片段 controller class

class ApplicationController < ActionController::Base
   include Cesid::Application
end

Cesid > Application.rb

module Cesid
  module Application
    extend ActiveSupport::Concern

    included do
      before_action :track_marketing_suite_cesid, only: [:new]
    end

    private

    def track_marketing_suite_cesid
      return unless id_token_available?
      ## @cesid_auth = Auth.new(@id_token)
      @cesid_auth = Auth.new(id_token)
      return unless @cesid_auth.present? && @cesid_auth.valid?
      @cesid_admin = Admin.where(email: @cesid_auth.email).first_or_initialize
    end

    def id_token_available?
      ## @id_token.present?
      id_token.present?
    end

    def id_token
      @id_token ||= id_token_param
    end

    def id_token_param
      cookies[:id_token]
    end
  end
end

現在,我正在嘗試為該方法創建一個簡單的單元測試

id_token_可用?

我只是想將id_token_param設置為隨機值。

我試過按照說明使用此代碼有沒有辦法用 Rspec 存根包含模塊的方法?

allow_any_instance_of(Cesid).to receive(:id_token_param).and_return('hello')

但我只是得到這個錯誤

NoMethodError:
   undefined method `allow_any_instance_of' for #<RSpec::ExampleGroups::CesidApplication::CesidAuthorizations::GetCesidApplication:0x00007fa3d200c1c0> Did you mean?  allow_mass_assignment_of

Rspec 檔案

require 'rails_helper'

describe Cesid::Application,  :type => :controller  do
  describe 'cesid application' do
    before do
      allow_any_instance_of(ApplicationController).to receive(:id_token_param).and_return('hello')
    end

    it 'returns true if the id_token is present' do
      expect(Cesid::Application.send('id_token_available?')).to eql(true)
    end
  end
end

Rspec版

3.5.4

老實說,這讓我發瘋了

我看到三個問題:

  1. 您在未定義的上下文中調用allow_any_instance_of allow_any_instance_of可以用在before塊中。 我需要更具體地查看您的 RSpec 代碼。

  2. 實際上,您的代碼是在ApplicationController上調用的,而不是在模塊上調用的,因此您需要將存根更改為

    allow_any_instance_of(ApplicationController).to receive(:id_token_param).and_return('hello')
  3. 目前根本不會調用id_token_param ,因為id_token_available? 檢查實例變量而不是調用id_token_paramid_token方法的返回值。 只需更改id_token_available? 到:

    定義 id_token_available? id_token.present? 結尾

有一個更好的方法來進行這個測試。 規范中的type: :controller元數據為您提供了一個匿名的 controller實例供您使用。 這是一個示例,說明如何編寫此代碼以實際測試是否使用了模塊中的before_action

describe Cesid::Application, type: :controller do
  controller(ApplicationController) do
    def new
      render plain: 'Hello'
    end
  end

  describe 'cesid before_action' do
    before(:each) do
      routes.draw { get 'new' => 'anonymous#new' }
      cookies[:id_token] = id_token
      allow(Auth).to receive(:new).with(id_token)
        .and_return(instance_double(Auth, valid?: false))
      get :new
    end

    context 'when id token is available' do
      let(:id_token) { 'hello' }

      it 'sets @cesid_auth' do
        expect(assigns(:cesid_auth)).to be_present
      end
    end

    context 'when id token is unavailable' do
      let(:id_token) { '' }

      it 'does not set @cesid_auth' do
        expect(assigns(:cesid_auth)).to be_nil
      end
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM