存根请求无效

时间:2017-06-21 08:34:52

标签: ruby-on-rails rspec request stub webmock

运行测试时,此webmock存根请求无效。这是控制器。

class AdvisorDashboardController < ApplicationController
  load_and_authorize_resource :class => 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? && @pending_transaction.blank? && !@semi_settled.blank?
          @active_transactions = @semi_settled['order_count'].to_i
        elsif !@transactions.blank? && !@pending_transaction.blank? && @semi_settled.blank?
          @active_transactions = @pending_transaction['order_count'].to_i
        elsif !@transactions.blank? && !@pending_transaction.blank? && !@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? && 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 << 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(&: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

这是Rspec控制器

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&advisor_id={id}"

        stub_request(:get, uri_template).
            with(headers: {'auth-token'=> '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&advisor_id={id}"

        stub_request(:get, uri_template).
            with(headers: {'auth-token'=> '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&advisor_id={id}"

        stub_request(:get, uri_template).
            with(headers: {'auth-token'=> '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

以上三个存根请求无效。而是从控制器调用api调用,我得到一个空数组作为响应。我正在使用的存根请求有什么问题吗?任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

stub_request通过net/http库发出的存根请求,但是您用来发出请求的Typhoeus包含了libcurl。由于这一点,而不是stub_request您需要使用Typhoeus.stub(...)来存根这些请求 - https://github.com/typhoeus/typhoeus#direct-stubbing