[英]how to test global variables with QUnit?
我是QUnit的新手,這是我正在使用的SAPUI5應用程序。
我的控制器中有以下代碼
sap.ui.define( ["sap/ui/core/mvc/Controller",
'BatchProcessing/model/ViewControls',
"sap/ui/core/routing/History",
'BatchProcessing/model/formatter',
'BatchProcessing/model/DataConnection',
'sap/m/MessageBox',
'sap/m/Dialog',
'sap/m/Button'
], function (Controller, ViewControls, History, formatter, dataConnector,
MessageBox, Dialog, Button) {
"use strict";
var detailsControls;
var data = [];
var rowData;
return Controller.extend("BatchProcessing.controller.Detailsview", {
onInit : function () {
var route = sap.ui.core.UIComponent.getRouterFor(this);
route.getRoute("page3").attachMatched(this.initController, this);
},
initController: function(testObj){
detailsControls = testObj || ViewControls.detailsViewControls.apply(this);
detailsControls.evalPriceButton.setEnabled(true);
detailsControls.awardQuantity.setEnabled(true);
detailsControls.buyQuantity.setEnabled(true);
detailsControls.priorUnitPrice.setEnabled(true);
detailsControls.proposedUnitPrice.setEnabled(true);
detailsControls.awardDate.setEnabled(true);
detailsControls.proposedUnitPrice.setValue('');
detailsControls.buyQuantity.setValue('');
detailsControls.priorUnitPrice.setValue('');
detailsControls.awardQuantity.setValue('');
detailsControls.awardDate.setValue('');
detailsControls.BZZPIIN = '';
detailsControls.PRC = '';
detailsControls.VENDOR = '';
detailsControls.CAGE = '';
detailsControls.ORDER_QUAN = '';
detailsControls.NETPRICE = '';
detailsControls.acceptBtn.setEnabled(false);
//retrieve data selected by user on previous page
rowData = ViewControls.getRowData();
ViewControls.setPriceChangeAccepted(false);//set changes accepted to 'Not Accepted'
data = dataConnector.getContractHistoryData(rowData[0].M_BIC_B_NSN);
this.setPriceModel(rowData);
this.setPaginationData();
},
這是我針對上述代碼的QUnit測試。 我只是想設置它。
QUnit.test("initController setup", function(assert) {
// Arrange
var setEnabled = sinon.spy();
var setValue = sinon.spy();
var getValue = sinon.spy();
var setVisible = sinon.spy();
var setVisibleRowCount = sinon.spy();
var setValueState = sinon.spy();
var getRowData = sinon.stub().returns([
{BAWAEFFDT: "20150213",
BZZPIIN: "SPE5E967V1599",
CAGE: "4R840",
CUR_QTY: "20",
FAIR_MKT_PRICE: "160.00",
INDEX: "3",
M_BIC_B_NSN: "306018040",
NETPRICE: "150.99",
ORDER_QUAN: "35",
PERC_DIFF: "20",
PROPOSED_PRICE: "25.99",
PriceReasonableCode: "BG",
VENDOR: "STAMCO."
}]);
var rowData = [
{BAWAEFFDT: "20150213",
BZZPIIN: "SPE5E967V1599",
CAGE: "4R840",
CUR_QTY: "20",
FAIR_MKT_PRICE: "160.00",
INDEX: "3",
M_BIC_B_NSN: "306018040",
NETPRICE: "150.99",
ORDER_QUAN: "35",
PERC_DIFF: "20",
PROPOSED_PRICE: "25.99",
PriceReasonableCode: "BG",
VENDOR: "STAMCO."
}];
var detailsControls = {
summaryTable:{
setVisibleRowCount: setVisibleRowCount
},
evalPriceButton: {
setEnabled: setEnabled
},
nextBtn:{
setVisible: setVisible,
setEnabled: setEnabled
},
prevBtn:{
setVisible: setVisible,
setEnabled: setEnabled
},
dropDown:{
getValue: getValue,
setValue: setValue
},
acceptBtn:{
setEnabled: setEnabled
},
awardQuantity:{
setValue: setValue,
getValue: getValue,
setEnabled: setEnabled,
setValueState: setValueState
},
missingFieldError:{
setVisible: setVisible
},
invalidFieldError: {
setVisible: setVisible
},
invalidDateError: {
setVisible: setVisible
},
buyQuantity: {
setEnabled: setEnabled,
setValue: setValue,
setValueState: setValueState
},
priorUnitPrice:{
setEnabled: setEnabled,
setValue: setValue,
setValueState: setValueState
},
proposedUnitPrice:{
setEnabled: setEnabled,
setValue: setValue,
setValueState: setValueState
},
awardDate:{
setEnabled: setEnabled,
setValue: setValue,
setValueState: setValueState
}
};
detailsController.initController(detailsControls);
});
我遇到的問題是,當我轉到下一行時,收到一個未定義的錯誤(rowData [0]未定義)。
data = dataConnector.getContractHistoryData(rowData[0].M_BIC_B_NSN);
我在函數中添加了以下幾行,以通過detailsControls解決此問題。 我通過testObj的唯一目的是使QUnit工作,或者被告知這樣做。 我認為這是不正確的,並且我不想在功能中添加更多參數,只是為了使測試正常進行。 我沒有找到類似的例子。 誰能告訴我一種使這項工作正確無誤的方法?
initController: function(testObj){
detailsControls = testObj ||
ViewControls.detailsViewControls.apply(this);
只需對返回數據的函數進行存根:
sinon.stub(BatchProcessing.model.ViewControls, 'detailsViewControls').returns(detailsControls)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.