如何解决单元测试中当时块的$ q.reject承诺?

时间:2015-03-27 19:40:49

标签: angularjs unit-testing

这是我的测试,但我遇到的问题是我的成功和拒绝功能都没有被击中。如果我用良好的数据解决Http承诺,那么我的成功函数被命中(然后抛出异常“这不应该被击中”)。我希望错误处理程序在坏数据输入时被命中,但它永远不会。我做错了什么?

it("should resolve to failure action", function(done){
    Service.load().then(function(){
        throw new Exception("This should not be hit");
    }, function(){
        done();
    });

    $http.post.resolve(badData);
});


// mean while, the Service.load function
load: function(){
    return this.getHttpPromise().then(function(data){
        // process our data
        if(data.isGood)
            return data;

        return $q.reject("Data is not good");
    });
}

1 个答案:

答案 0 :(得分:1)

过了一会儿,我设法想出来了。在我的单元测试中,我不需要调用$ rootScope。$ digest或$ scope。$ digest,而是需要调用$ timeout.flush();