AngularJS:在$httpBackend.flush()之前需要$digest
AngularJS: $digest required before $httpBackend.flush()
我正试图理解为什么我需要在$httpBackend.flush()
之前调用$rootScope.$digest()
以使我的测试通过。
如果我不这样做,我将得到Error: No pending request to flush!
下面是我的测试:
it('should post a new task to the server', function() {
$httpBackend.when('POST', $rootScope.serverRoot + '/task/create').respond({});
var created = false;
mockBoardService.addTask({name: 'Add dynamic categories'})
.then(function(response) {
created = true;
}
);
$rootScope.$digest();
$httpBackend.flush();
expect(created).toBe(true);
})
和它正在调用的服务函数:
this.addTask = function(data) {
return $http.post($rootScope.serverRoot + '/task/create', data);
}
为什么需要运行$rootScope.$digest
看起来像是mockBoardService。addTask正在执行一些异步工作。如果没有$digest,它会在异步代码有机会发出该请求之前尝试刷新()$httpBackend请求。$ digest()调用给了它时间来完成异步工作。正确的方法(在Jasmine 2.0中,在1.3中略有不同)是这样做的:
it('should post a new task to the server', function(done) {
$httpBackend.when('POST', $rootScope.serverRoot + '/task/create').respond({});
var created = false;
mockBoardService.addTask({name: 'Add dynamic categories'})
.then(function(response) {
created = true;
done();
}
);
$httpBackend.flush();
expect(created).toBe(true);
})
注意'it'中传递给函数的'done'
看到http://jasmine.github.io/2.0/introduction.html section-Asynchronous_Support
相关文章:
- httpBackend模拟json文件响应
- Angular-错误:达到10$digest()迭代.正在中止
- 如何检查$httpBackend中是否未进行GET调用
- 如何用备用模拟服务器替换$httpBackend
- 数组过滤器导致无限$digest循环
- 如何避免$digest正在进行中的错误
- 角度达到 10 次 $digest() 迭代
- 将$scope对象作为参数提供给$digest中的观察者背后的逻辑
- 单元测试接收到带有$httpBackend的二进制响应
- 没有$scope.digest,$watch不会触发
- ng递归地包括整个index.html;达到“10$digest()迭代”;
- $digest已在进行中
- AngularJS错误:$rootScope:infdig Infinite$digest循环:达到10次$digest
- Angular-使用httpBackend测试http服务会引发意外异常
- 如何使用 HttpBackend 进行$http
- $digest() updating parent $rootScope
- angularjs 无限$digest 当范围没有变化时循环
- 类型错误: $httpBackend未定义
- Angularjs 单元测试和 httpbackend 超时
- AngularJS:在$httpBackend.flush()之前需要$digest