用 Jasmine 单元测试 AngularJS Service 遇到的一个问题.

2016-01-28 21:15:47 +08:00
 97world

首先, 有 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, 网上查了一下, 并没有找到类似我这种做法的解决方法.

希望能够得到帮助.

2081 次点击
所在节点    程序员
1 条回复
hantsy
2016-01-29 11:20:09 +08:00
Add Angularjs-mocks, and mock the dependencies in test method.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/254089

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX