小编Per*_*oug的帖子

Angular 1.1.5测试基于承诺的服务

我们使用karma对我们的角度服务进行单元测试,这些服务包含$ http调用,因此我们有一个模拟的$ httpbackend,所以我们可以在没有server和db的情况下运行应用程序.这工作正常,服务可以调用$ 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;
}
Run Code Online (Sandbox Code Playgroud)

模拟的$ httpbackend

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

考试

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);
}); …
Run Code Online (Sandbox Code Playgroud)

angularjs karma-runner

19
推荐指数
1
解决办法
6573
查看次数

标签 统计

angularjs ×1

karma-runner ×1