【问题标题】:How do you resolve a $q.reject promise from a then block in unit testing?您如何解决单元测试中来自 then 块的 $q.reject 承诺?
【发布时间】:2015-03-27 19:40:48
【问题描述】:

这是我的测试,但我遇到的问题是我的成功和拒绝功能都没有受到影响。如果我用良好的数据解决了 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");
    });
}

【问题讨论】:

    标签: angularjs unit-testing


    【解决方案1】:

    看起来过了一段时间我设法解决了这个问题。在我的单元测试中,我不需要调用 $rootScope.$digest 或 $scope.$digest,而是需要调用 $timeout.flush();

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2018-10-30
      • 2013-02-09
      • 2019-09-01
      相关资源
      最近更新 更多