我知道这个错误很多都出现在问题中,但尽管我付出了努力,但我没有设法解决我的问题。
我有一个服务从Parse获取一些类别。这在运行我的测试时成功完成,但我收到标题中显示的错误。
我的代码如下:
ParseService.js
angular.module('starter').service('ParseService', ['$q', function ($q) {
this.getAllCategories = function() {
var deferred = $q.defer();
var categories = [];
var query = new Parse.Query("Category");
query.find({
success: function(results) {
console.log('Succesfully queried Parse.');
for (var i = 0; i < results.length; i++) {
var result = results[i];
categories.push(result.get("name"));
}
deferred.resolve(categories);
console.log(categories);
},
error: function(error) {
console.log('Error querying Parse.');
console.log(error);
deferred.reject(error);
}
});
return deferred.promise;
};
}]);
ParseServiceTest.js
describe('ParseService', function(){
var service;
beforeEach(function(){
module('starter');
});
it ('should fetch Categories', function(done){
beforeEach(inject(function(ParseService){
service=ParseService;
}));
var promise = service.getAllCategories();
promise.then(function(categories){
expect(categories.length).toEqual(5);
done();
});
});
});
现在控制台输出如下:
'Succesfully queried Parse.'
['Pets', 'Automobile', 'Social', 'Retail', 'Technology']
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
似乎服务成功返回类别,但由于某种原因,从未执行done()方法。
你可以忽略我没有模拟对Parse的调用的事实。我知道这不是最好的做法。
答案 0 :(得分:1)
要在测试中解析/拒绝$ q promises,您需要启动摘要周期。
describe('ParseService', function() {
var service;
var $rootScope;
beforeEach(module('starter'));
beforeEach(inject(function(ParseService, _$rootScope_) {
service = ParseService;
$rootScope = _$rootScope_;
}));
it('should fetch Categories', function(done) {
var promise = service.getAllCategories();
promise.then(function(categories) {
expect(categories.length).toEqual(5);
done();
});
// run digest cycle to resolve $q promises
$rootScope.$digest();
});
});