V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
97world
V2EX  ›  程序员

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

  •  
  •   97world · 2016-01-28 21:15:47 +08:00 · 2084 次点击
    这是一个创建于 3222 天前的主题,其中的信息可能已经有所发展或是发生改变。

    首先, 有 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 条回复    2016-01-29 11:20:09 +08:00
    hantsy
        1
    hantsy  
       2016-01-29 11:20:09 +08:00
    Add Angularjs-mocks, and mock the dependencies in test method.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2705 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:08 · PVG 14:08 · LAX 22:08 · JFK 01:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.