Angular 1.1.5测试基于承诺的服务

时间:2013-07-02 07:15:36

标签: angularjs karma-runner

我们使用karma对我们的角度服务进行单元测试,这些服务包含$ http调用,因此我们有一个模拟的$ httpbackend,所以我们可以在没有server和db的情况下运行app。 这工作正常,服务可以调用$ http(" someurl?id = 1234"),我们得到正确的数据。

但是当我们尝试在单元测试中做同样的事情时,我们无法让它工作,承诺永远不会解决,当它涉及$ http

服务:

getAllowedTypes: function (contentId) {
    var deferred = $q.defer();
    $http.get(getChildContentTypesUrl(contentId))
        .success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
        error(function (data, status, headers, config) {
            deferred.reject('Failed to retreive data for content id ' + contentId);
        });
    return deferred.promise;
}

模拟的$ httpbackend

$httpBackend
   .whenGET(mocksUtills.urlRegex('/someurl'))
   .respond(returnAllowedChildren); //returns a json object and httpstatus:200

测试

it('should return a allowed content type collection given a document id', function(){

    var collection;
    contentTypeResource.getAllowedTypes(1234).then(function(result){
        collection = result;
    });

    $rootScope.$digest();

    expect(collection.length).toBe(3);
});

但是未定义集合,从不调用.then()。

尝试了很多东西以获得解决的承诺,$ rootScope。$ apply(),$ digest,$ httpBacke.flush(),但没有任何作用

所以模拟$ httpBackend在应用程序中从控制器调用时有效,但在karma单元测试中直接调用服务时则无效

3 个答案:

答案 0 :(得分:7)

你不需要消化两次,因为$ httpBackend.flush()会调用digest本身。 你必须拨打电话,调用摘要来解析请求拦截器,调用flush。

这是一个有效的Plnkr:http://plnkr.co/edit/FiYY1jT6dYrDhroRpFG1?p=preview

答案 1 :(得分:4)

在你的情况下,你需要$ digest两次,一次是$ httpBackend,再次是你自己的延迟。

所以:

it('should return a allowed content type collection given a document id', function(){

    var collection;
    contentTypeResource.getAllowedTypes(1234).then(function(result){
        collection = result;
    });
    $httpBackend.flush();
    $rootScope.$digest();

    expect(collection.length).toBe(3);
});

答案 2 :(得分:4)

你快到了。在您的情况下,您只需要在刷新HTTP后端之前强制执行摘要循环。请参阅下面的示例代码。

it('should return a allowed content type collection given a document id', function(){

    var collection;
    contentTypeResource.getAllowedTypes(1234).then(function(result){
        collection = result;
    });

    $rootScope.$digest();
    $httpBackend.flush();
    expect(collection.length).toBe(3);
});