首先, 有 3 个 Service
var service = angular.module('uxServices', ['ngResource']);
service.factory('promiseGet', ['$http','$q','toaster', function($http,$q,toaster){
return function(url,isXML){
var deferred = $q.defer();
var req;
if (isXML != null && isXML == true) {
req = {
method: 'GET',
url: url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/xml'
}
};
} else {
req = {
method: 'GET',
url: url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
}
$http(req)
.success(function(result){
if(angular.isDefined(result.code) && result.code==-1){
toaster.pop('error', result.title, result.msg);
deferred.reject(result.msg);
}else{
deferred.resolve(result);
}
})
.error(function(reason){
if(uxApp.settings.debug)
toaster.pop('error', 'Server Error', reason);
deferred.reject(reason);
});
return deferred.promise;
};
}]);
service.factory('GetDataFromTAPI', ['promisePost', function (promisePost) {
return function (para){
return promisePost('/bdna/ux/getDataFromTAPI', para);
};
}]);
service.factory('TechnopediaContentPackService', ['$q', 'GetDataFromTAPI', function ($q, GetDataFromTAPI) {
var typeMap = {
'RELEASE': 'SW Release',
'HWMODEL': 'HW Model',
'CPUMODEL': 'CPU Model'
};
var result = {
getContentPackList: getContentPackList,
getContentPackInfo: getContentPackInfo
};
return result;
function getContentPackList (t_type) {
var type4API = typeMap[t_type];
var deferred = $q.defer();
var para = {
'url': encodeURI("/odata/BDNA_CP_UI?$select=UI_CP_NAME&$expand=BDNA_CP($select=CP_NAME)&$filter=DRIVER_ENTITY eq '" + type4API + "' and BDNA_CP/IS_SUBSCRIBED eq 'Y'"),
'data': '',
'action': 'GET_CONTENT_PACK_LIST',
'comment': 'Get Content Pack List'
};
if (!type4API) {
deferred.resolve(null);
}
GetDataFromTAPI(para).then(function (result) {
var cpList = result.value;
if (cpList && angular.isArray(cpList)) {
deferred.resolve(mergeConfFromUI(cpList, uxApp.CP_MAP[t_type]));
}
}, function (msg) {
deferred.reject(msg);
});
return deferred.promise;
};
function getContentPackInfo (cpName) {
};
function mergeConfFromUI (cpList, conf) {
var listIndex = cpList.length;
var pushedNames = [];
var result = [];
while (listIndex --) {
var cpItem = cpList[listIndex];
var cpName = cpItem.UI_CP_NAME;
var confItem = conf[cpName];
if (confItem) {
confItem.UI_CP_NAME = cpName;
confItem.isDisabled = false;
(pushedNames.indexOf(cpName) < 0) && result.push(confItem) && pushedNames.push(cpName);
} else {
confItem = {};
confItem.UI_CP_NAME = cpName;
confItem.isDisabled = true;
result.push(confItem);
}
}
return result;
}
}]);
我需要测试 TechnopediaContentPackService 这个 Service, 但是因为这个 Service 里面注入了 GetDataFromTAPI 这个 Service, 而 GetDataFromTAPI 里面又注入 promiseGet, 而且这 3 个 Service 都是返回 promise, 网上查了一下, 并没有找到类似我这种做法的解决方法.
希望能够得到帮助.
1
hantsy 2016-01-29 11:20:09 +08:00
Add Angularjs-mocks, and mock the dependencies in test method.
|