此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 [email protected]? && @pending_transaction.blank? && [email protected]_settled.blank?
@active_transactions = @semi_settled['order_count'].to_i
elsif [email protected]? && [email protected]_transaction.blank? && @semi_settled.blank?
@active_transactions = @pending_transaction['order_count'].to_i
elsif [email protected]? && [email protected]_transaction.blank? && [email protected]_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 [email protected]_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 [email protected]_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: '[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
上述三個存根請求不工作。而是從控制器調用api調用,並獲得一個空數組作爲響應。我使用的存根請求有什麼問題嗎?任何人都可以幫我解決這個問題嗎?
在你的控制器中的這個被稱爲? '「https://dev-alb-xxx.yyyyyyy.com.au/api/advisor?type=latest_transactions&advisor_id={id}」''''''''''''''''''''''''''''''''在這個URL上做了這樣的請求,但是我看不到你在哪裏使用它,至少在你的控制器代碼上面顯示了什麼。之後請更新您的問題,然後添加您從控制器調用該URL的代碼。 –
here .. @transactions = Service.new(session_email:current_user.email,user:current_user.user_id).latest_transactions –
在上面的rspec'it'示例中,您是否可以暫時註釋掉stubbing請求?並且暫時在'get:index,params:{........'上面插入這一行'WebMock.disable_net_connect!',然後重新運行該特定'it'示例的rspec。你應該看到一個rspec錯誤消息,比如'WebMock :: NetConnectNotAllowedError:...你可以用下面的代碼片斷來存儲這個請求:'。那麼你可以添加該錯誤消息作爲你的問題的一部分。這對我們很有幫助,所以我們可以知道請求的確切格式。 –