title: Stub Request is not working
body: <p>This webmock stub request is not working while running the test. Here is the controller.</p>
  
  <pre><code>class AdvisorDashboardController &lt; ApplicationController
    load_and_authorize_resource :class =&gt; AdvisorDashboardController
    require 'Numeric.rb'
    include ApplicationHelper
  
    def index   
        process_equity_data
        # Get transaction details from OP service
        @indices_data = MarketDatum.where("category = 'Indices'")
        @transactions = Service.new(session_email: current_user.email, user: current_user.user_id).latest_transactions
        unless @transactions.blank?
          @transactions.each do |transaction|
            case transaction['order_type']
            when 'PENDING'
              @pending_transaction =  transaction
            when 'SEMI_SETTLED'
              @semi_settled = transaction
            when 'SETTLED'
              @settled = transaction
            end
          end
          if !@transactions.blank? &amp;&amp; @pending_transaction.blank? &amp;&amp; !@semi_settled.blank?
            @active_transactions = @semi_settled['order_count'].to_i
          elsif !@transactions.blank? &amp;&amp; !@pending_transaction.blank? &amp;&amp; @semi_settled.blank?
            @active_transactions = @pending_transaction['order_count'].to_i
          elsif !@transactions.blank? &amp;&amp; !@pending_transaction.blank? &amp;&amp; !@semi_settled.blank?
            @active_transactions = @pending_transaction['order_count'].to_i + @semi_settled['order_count'].to_i
          end
        end
        unless session[:service_config].blank?
          rows_m2id = []
          service_code = helpers.service_type(Constant::TDREMINDER, session[:service_config])
          unless service_code.blank? &amp;&amp; service_code['service_provider'].blank?
             @term_deposit = OrderpadService.new(session_email: current_user.email, service_code: service_code['service_provider'], advisor_id: current_user.user_id).term_deposit
              if !@investers_list.blank?
              @investers_list.each do |investor_m2id|
                rows_m2id &lt;&lt; investor_m2id['m2id']
              end
            end
           @term_deposit = OpenTdList.select('open_td_list_id, investor_entity_m2id, external_entity_id, investor_search_name, asset_m2id, asset_name, asset_reference, issuer_name, market_value, holding_ccy, maturity_date')
                                        .where("external_entity_id IN (?)", rows_m2id)
          end
        end
  
        respond_to do |format|
          format.html
          format.csv do
            log('info', current_user.email, 'File creating for transaction list..')
            headers['Content-Disposition'] = 'attachment; filename="transaction-list"'
            headers['Content-Type'] ||= 'text/csv'
          end
        end
        log('info', current_user.email, 'retrieving market indices..')
        @indices_data = MarketDatum.where("category = 'Indices'")
    end
  
    def process_equity_data
      # Get availbale funds
      @equity_api = Service.new(session_email: current_user.email, user: current_user.user_id).equity_lists
      unless @equity_api.blank?
        log('info', current_user.email, 'retrieving market indices..')
        @watchlist_equity = MarketDatum.select('market_data.code, wat.created_at,market_data.updated_at')
                                       .joins('JOIN watchlists wat ON wat.code = market_data.code')
                                       .where('wat.created_by = ? AND market_data.status = 1', current_user.email)
                                       .order('wat.created_at DESC')
        log('info', current_user.email, 'retrieving watchlists..')
        @advisor_equity = Watchlist.where('created_by = ?', current_user.email)
        if !@advisor_equity.blank?
          @list = @advisor_equity.map(&amp;:code)
          unless @equity_api.blank?
            @watchlist_equity_api = @equity_api.reject { |x| @list.include? x['code'] }
          end
        else
            @watchlist_equity_api  = @equity_api
        end
  
      end
    end
  
  end
  </code></pre>
  
  <p>Here is the Rspec Controller</p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe AdvisorDashboardController, type: :controller do
    login_dg_advisor
    let(:users) {  create :user, :users_list  }
    let(:watchlist) { create :watchlist, :equity  }
  
    describe 'GET #index' do
      it 'success' do
        expect(response).to be_success
      end
  
      it 'successfully renders the index template' do
        get :index, params: { called_by: 'index', id: users.user_id }
  
        expect(response).to render_template(:index)
      end
  
      context 'index with no ajax' do
        before(:each) do
          response =  '[
                            {
                              "order_id": "12",
                              "order_type": "PENDING",
                              "order_count": 103
                            },
                            {
                              "order_id": "13",
                              "order_type": "SEMI_SETTLED",
                              "order_count": 2
                            },
                            {
                              "order_id": "14",
                              "order_type": "SETTLED",
                              "order_count": 41
                            }
                         ]'
  
          uri_template = Addressable::Template.new "https://dev-alb-xxx.yyyyyyy.com.au/api/advisor?type=latest_transactions&amp;advisor_id={id}"
  
          stub_request(:get, uri_template).
              with(headers: {'auth-token'=&gt; 'f940625eb8139da7c50bfe39e85e0e7366fdslgkjdflgkjdfdlfkgjfdb97115029233933e19f02541f57db78b6356183b7d04116b937af3fae81f1cfddb606'}).
              to_return(status: 200, body: response, headers: {})
        end
  
        it 'successfully renders the index template' do
          get :index, params: { id: users.user_id,created_by: 'arvind@gmail.com' }
          expect(response).to render_template(:index)
        end
      end
  
      context 'pending trasnactions blank and semi-settled not blank' do
        before do
          response =  '[
                            {
                              "order_id": "13",
                              "order_type": "SEMI_SETTLED",
                              "order_count": 2
                            },
                            {
                              "order_id": "14",
                              "order_type": "SETTLED",
                              "order_count": 41
                            }
                         ]'
  
          uri_template = Addressable::Template.new "https://dev-alb-xxx.yyyyyyy.com.au/api/advisor?type=latest_transactions&amp;advisor_id={id}"
  
          stub_request(:get, uri_template).
              with(headers: {'auth-token'=&gt; 'f940625eb8139da7c50bfe39e85e0e7366fdslgkjdflgkjdfdlfkgjfdb97115029233933e19f02541f57db78b6356183b7d04116b937af3fae81f1cfddb606'}).
              to_return(status: 200, body: response, headers: {})
        end
  
        it 'successfully renders index template' do
          get :index, params: { called_by: 'market_indices', id: users.user_id, created_by: 'arvind@gmail.com' }
          expect(response).to render_template(:index)
        end
  
      end
  
      context 'pending trasnactions not blank and semi-settled is blank' do
        before do
          response =  '[
                            {
                              "order_id": "12",
                              "order_type": "PENDING",
                              "order_count": 103
                            },
                            {
                              "order_id": "14",
                              "order_type": "SETTLED",
                              "order_count": 41
                            }
                        ]'
  
          uri_template = Addressable::Template.new "https://dev-alb-xxx.yyyyyyy.com.au/api/advisor?type=latest_transactions&amp;advisor_id={id}"
  
          stub_request(:get, uri_template).
              with(headers: {'auth-token'=&gt; 'f940625eb8139da7c50bfe39e85e0e7366fdslgkjdflgkjdfdlfkgjfdb97115029233933e19f02541f57db78b6356183b7d04116b937af3fae81f1cfddb606'}).
              to_return(status: 200, body: response, headers: {})
        end
  
        it 'successfully renders index template' do
          get :index, params: { called_by: 'market_indices', id: users.user_id, created_by: 'arvind@gmail.com' }
          expect(response).to render_template(:index)
        end
  
      end
  
    end
  end
  </code></pre>
  
  <p>The above three stub requests are not working. Instead the api call is called from the controller and I get an empty array as the response. Is there anything wrong with the stub request that I am using? Can anyone help me to solve this problem?</p>