【问题标题】:Angular httpBackend: No pending request to flushAngular httpBackend:没有待处理的刷新请求
【发布时间】:2014-05-28 21:25:38
【问题描述】:

我正在尝试使用$httpBackend 来测试我的AngularJS 应用程序。但是,它一直告诉我我没有待处理的请求。

这是测试:

it('should attempt to get boards from server', function() {
    httpBackend.when('GET', rootScope.serverRoot + '/boards').respond({id: 1});

    var boards;
    BoardServiceInstance.getBoards().then(function(data) {
        boards = data;
    });

    httpBackend.flush();

})

还有服务:

this.getBoards = function() {
    return $http.get($rootScope.serverRoot + '/boards');
}

我做错了什么?

【问题讨论】:

  • 我觉得不错。这是a fiddle 似乎有效。也许看看你是否能发现小提琴和其他代码之间的区别?

标签: javascript angularjs unit-testing testing jasmine


【解决方案1】:

添加 $rootScope.$digest(); 成功了。

这是最终代码:

it('should attempt to get boards from server', function() {
    $httpBackend.when('GET', '/boards').respond({id: 1});

    var boards;
    mockBoardService.getBoards().then(function(data) {
        boards = data;
    });

    $rootScope.$digest();

    $httpBackend.flush();

})

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多