我正在尝试使用Jasmine异步测试来测试包装在AngularJS服务中的REST API。我知道,我应该也正在服务器上运行应用程序接口测试,并使用模拟$httpBackend来测试服务,但我仍然想知道如何在需要的地方进行异步测试。
我遇到的问题是我的延迟(由$http返回)似乎永远不能解决。跳过该服务并尝试简单地使用$http也存在同样的问题。这里我做错了什么:
describe('Jasmine + Angular + $http', function() {
var injector;
beforeEach(function() {
injector = angular.injector(['ng']);
});
it('can be tested', function() {
injector.invoke(function($http) {
var complete = false;
runs(function() {
$http.get('...').then(function(res) {
complete = true;
});
});
waitsFor(function() {
return complete;
}, 'query to complete', 5000);
runs(function() {
expect(complete).toEqual(true);
});
});
});
});(我使用grunt-contrib-jasmine来运行测试)
发布于 2013-08-12 23:38:01
除非您调用$httpBackend.flush(),否则HTTP请求将不会触发。
更多信息可在此处找到:http://docs.angularjs.org/api/ngMock.$httpBackend
https://stackoverflow.com/questions/16822387
复制相似问题