这是我正在测试的功能
self.getNewTaskCnt = function(newP) {
var newIdx,
newTaskCntPromise = [];
for (newIdx= 0; newIdx< newP.length; newIdx++) {
newTaskCntPromise.push(self.getTaskCnt(newP[newIdx]));
}
Promise.allSettled(newTaskCntPromise)
.then(function(reply) {
for (newIdx= 0; newIdx< newP.length; newIdx++) {
self.updateIcon(reply[newIdx].value);
}
}, function() {
WorklistHelper.addToLog("failure", "failure to get task details in taskService");
});
};
这是单元测试
it('calls updateIcon for each new task added', (done) => {
var newP = [{ID: 1, pId:1, eId:1, schId:1},
{ID: 2, pId:2, eId:2, schId:2}],
taskPromise = $q.defer();
spyOn(taskService, "getTaskCnt").and.callFake(function() {
taskPromise.resolve(42);
return taskPromise.promise;
});
var updateCallCount = spyOn(taskService, "updateIcon").and.callFake(function() {
expect(taskService.updateIcon).toHaveBeenCalled();
done();
});
taskService.getNewTaskCnt(newP)
$scope.$apply();
});
我确实查看了此问题的其他示例,它们似乎主要集中在调用函数后的 $scope.$apply() 或 $scope.$digest() 调用上。我尝试过并延长 jasmine 超时,但它们似乎没有解决在函数中调用的 promise.allSettled。感觉我遗漏了一些非常明显的东西。