【问题标题】:Jasmine won't test an angular service with multiple nested promisesJasmine 不会测试具有多个嵌套承诺的角度服务
【发布时间】:2025-12-22 17:10:12
【问题描述】:

我正在尝试为 Angular 服务编写一个 jasmine 测试用例。因为我必须调用 PouchDB 并获取 http 资源,所以我使用 $q。

在我的应用程序中,我有两个数据源:作为 localStorage 的 PouchDB 和通过 http 请求的 XML 文档。我设法为 localStorage.service 和 XMLparser.service 编写了测试,没有太多麻烦(我必须做一个 50 毫秒的超时,然后调用 $rootScopre.$digest())。

但是,当我尝试为使用 localStorage 和 XMLParser 的服务运行测试时,我就是无法让它通过。 Karma 挂起 5 秒,测试在超时时失败。如果我在任何地方添加一些 console.log() ,所有代码实际上都会被执行。就像 done() 没有关闭测试用例一样。如果我直接在我的应用程序中运行代码,它就可以工作。

我的代码中发生了很多事情,所以我会尝试解释一下:

function loadCategories(){
    var deferred = $q.defer();

    //Json structure that tells my parser what to extract from the XML
    var structure = { ... } 

    //Check in the PouchDB to see if the structure is already cached
    localStorage.getStructure() 
    .then(function(struct){
        deferred.resolve(struct.category);
    })
    .catch(function(e){
        if(e.name != 'not_found') return deferred.reject(e);

        //If the structure is not cached, get it from the parser through an http request
        xmlparser.readFile(apiUrl, structure) 
        .then(function(doc){
            localStorage.saveStructure(doc); //Save it in the PouchDB
            deferred.resolve(doc.category); //Resolve with the categories
        })
        .catch(function(e){
            deferred.reject(e);
        });
    });

    return deferred.promise;
}

这是我的测试方法:

it('should list the categories', function(done){
    //The answer to this call is mocked in the beforeEach
    $httpBackend.expectGET('/lib/xmlDocuments/indicator.xml');

    var promise = Indicator.getCategories()
    .then(function(categories){
        expect(categories[0].name).toBe('Emplois par secteur d\'activité');
        expect(categories[1].name).toBe('Emplois par niveau de compétence');
        done();
        //When i put a console.log here it get executed...
    })
    .catch(function(e){
        expect(e).toBe(null);
    });

    //Here i tryed to force a $digest and then flush() the requests
    //but i still get "Async callback was not invoked within timeout"
    setTimeout(function() {
        $scope.$digest();
        setTimeout(function(){
            $httpBackend.flush();
        }, 50);
    }, 50);
});

现在我很确定有些事情我做得不好,但我不能指手画脚:

  • 我是否误用了承诺?
  • 我应该模拟 XMLParser 和 localStorage 吗?
  • 我的茉莉花有错吗?
  • 我是否过度使用了 $q?

感谢您的帮助!

【问题讨论】:

  • 这不会解决问题,但您不需要在服务中使用 $q。您应该能够返回 localStorage.getStructure(),因为它返回了一个承诺。如果这是一个异步请求,那么你应该模拟它。
  • 好吧,我不能简单地返回 localStorage.getStructure(),因为如果结构不在本地数据库中,我需要调用 xmlParser.readFile()?我错过了什么吗?
  • 如果你从一个 Promise 内部返回一个 Promise,它会继续这个链。你也需要回xmlParser.readFile
  • 它会给出类似的东西,但它在两种情况下都可以保存吗? return localStorage.getStructure() .then(function(struct){ console.log('Struct was found'); return struct; }) .catch(function(e){ console.log('Struct was not found'); if(e.name != 'not_found') return e; return xmlparser.readFile(apiUrl, structure) }).then(function(doc){ console.log('I am saving in the database'); localStorage.saveStructure(doc); return doc.category; });

标签: angularjs jasmine karma-jasmine


【解决方案1】:

好的,我最终模拟了 XMLParser 和 localStorage。我得出的结论是,再次重新测试这些服务是没有意义的。这是一个单元测试,应该只测试一个单元。

我还按照 Explosion Pills 的建议修改了我对 Promise 的使用。

//Load the categories
function loadCategories(){
    var structure = { ... }

    return localStorage.getStructure()
    .then(function(struct){
        return struct.category;
    })
    .catch(function(e){
        if(e.name != 'not_found') return e;

        return xmlparser.readFile(apiUrl, structure)
        .then(function(doc){
            localStorage.saveStructure(doc);
            return doc.category;
        });
    });
}

我像以前一样添加了模拟

beforeEach(module('gisMobile', function($provide){
    xmlParserMock = {
        readFile: function(){
            var defer = $q.defer();
            defer.resolve(indicatorJsonMock);
            return defer.promise;
        }
    };

    localStorageMock = {
        isStructureCached: false,
        getStructure: function(){
            var defer = $q.defer();
            if(localStorageMock.isStructureCached)
                defer.resolve(indicatorJsonMock);
            else
                defer.reject({name: 'not_found'});
            return defer.promise;
        },
        saveStructure: function(){
            localStorageMock.isStructureCached = true;
        }
    }

    $provide.value('xmlparser', xmlParserMock);
    $provide.value('localStorage', localStorageMock);
}));

最后,测试现在看起来像

it('should list the categories', function(done){
    var promise = Indicator.getCategories()
    .then(function(categories){
        expect(categories[1].name).toBe('Emplois par secteur d\'activité');
        expect(categories[0].name).toBe('Emplois par niveau de compétence');
        done();
    })
    .catch(function(e){
        expect(e).toBe(null);
    });

    $rootScope.$digest();
});

所以要结束这个问题,我的假设是正确的:

  • 我没有正确使用承诺
  • 我不得不模拟 xmlParser 和 localStorage,因为它们不是本次测试的目标。

【讨论】: