Reputation: 1651
This webmock stub request is not working while running the test. Here is the controller.
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 [email protected]? && @pending_transaction.blank? && !@semi_settled.blank?
@active_transactions = @semi_settled['order_count'].to_i
elsif [email protected]? && !@pending_transaction.blank? && @semi_settled.blank?
@active_transactions = @pending_transaction['order_count'].to_i
elsif [email protected]? && !@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
Here is the Rspec Controller
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: '[email protected]' }
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: '[email protected]' }
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: '[email protected]' }
expect(response).to render_template(:index)
end
end
end
end
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?
Upvotes: 2
Views: 2424
Reputation: 49890
stub_request
stubs requests made via the net/http
library, however Typhoeus, which you're using to make requests, wraps libcurl. Beacuse of that, rather than stub_request
you need to be using Typhoeus.stub(...)
to stub those requests - https://github.com/typhoeus/typhoeus#direct-stubbing
Upvotes: 1