gpt4 book ai didi

javascript - $httpBackend.flush() 进入无限循环

转载 作者:行者123 更新时间:2023-11-28 08:16:10 26 4
gpt4 key购买 nike

我想要彻底,所以请耐心等待,这里会有很多内容。我们有一个远程日志服务功能,可以在我们需要时向我们发送一些客户端信息。像这样的事情:

callHome: function(message){
var deferred, promise;
try{
if (someService.getRemoteLoggingEnabled())
{
//collect all the info into remoteLog
promise = $http.post("Logging", remoteLog);
wipeLog();
}
else
{
deferred = $q.defer();
promise = deferred.promise;
deferred.resolve();
}
}
catch(error)
{
try{
if (!promise)
{
deferred = $q.defer();
promise = deferred.promise;
}
deferred.reject(error.message);
}
catch(e2){}
}
return promise;
}

在实际应用程序中运行时,这一切都工作得很好。当尝试为其编写单元测试时,问题就出现了。我对何时未启用远程日志记录以及何时出现错误进行了测试。这些看起来像这样:

it ("should resolve the promise with nothing when remote logging is turned off", inject(function($rootScope) {
remoteLoggingEnabled = false; //this is declared above a beforeEach that mocks getRemoteLoggingEnabled
var successSpy = jasmine.createSpy("success");
var failSpy = jasmine.createSpy("fail");
var promise = loggingService.callHome("Hello World");
promise.then(successSpy, failSpy);
$rootScope.$digest();

expect(successSpy).toHaveBeenCalledWith(undefined);
expect(failSpy).not.toHaveBeenCalled();
}));
it ("should reject the promise when there is an error with the error message", inject(function($rootScope) {
remoteLoggingEnabled = true;
var successSpy = jasmine.createSpy("success");
var failSpy = jasmine.createSpy("fail");
//angular.toJson is called while it's gathering client-side info
spyOn(angular, "toJson").andCallFake(function() {throw new Error("This is an error");});
var promise = loggingService.callHome("Hello World");
promise.then(successSpy, failSpy);
$rootScope.$digest();

expect(successSpy).not.toHaveBeenCalled();
expect(failSpy).toHaveBeenCalledWith("This is an error");
}));

这些效果很好。接下来我想添加测试,以了解它何时实际发出请求。我做了一个这样的测试:

it ("should resolve the promise with the http info when it makes a successful request", inject(function($rootScope, $httpBackend) {
remoteLoggingEnabled = true;
var successSpy = jasmine.createSpy("success");
var failSpy = jasmine.createSpy("fail");
$httpBackend.expect("POST", new RegExp("Logging"), function(jsonStr){
//not concerned about the actual payload
return true;
}).respond(200);
var promise = loggingService.callHome("Hello World");
promise.then(successSpy, failSpy);
$httpBackend.flush();
$rootScope.$digest();

expect(successSpy).toHaveBeenCalledWith(/*http info*/);
expect(failSpy).not.toHaveBeenCalled();
}));

但是,这个测试只是挂起。我单步执行了代码,它陷入了 $httpBackend.flush()$rootScope.$digest() 调用中,特别是在这个 while 循环中:

      while(asyncQueue.length) {
try {
asyncTask = asyncQueue.shift();
asyncTask.scope.$eval(asyncTask.expression);
} catch (e) {
clearPhase();
$exceptionHandler(e);
}
lastDirtyWatch = null;
}

我已经检查了 asyncTask.expression 的循环情况,但我找不到它正在执行的操作的任何模式。

我仍在掌握 Promise 以及如何使用它们,所以我希望我在这里做的事情根本上是错误的。任何帮助将不胜感激。

最佳答案

问题出在我的测试设置中(未作为问题的一部分显示)。只要出现错误,就会通过经过装饰的 $exceptionHandler 调用此 callHome 函数。在 callHome 测试期间出现错误,因此再次调用它,然后从那里循环。我修复了该错误,现在一切正常。

关于javascript - $httpBackend.flush() 进入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23569121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com