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

时间:2014-05-09 16:01:14

标签: javascript angularjs unit-testing jasmine promise

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

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();
}));

然而,这个测试只是挂起。我逐步完成了代码,它被$rootScope.$digest()调用$httpBackend.flush(),特别是在这个while循环中:

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

我已经检查了asyncTask.expression,但是我找不到任何模式。

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

1 个答案:

答案 0 :(得分:0)

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

相关问题