[英]Test Angular Service that Returns a $q Promise in Jasmine 2.0 Asynchronously
我有一个测试承诺结果的规范。
承诺解决,但then
, catch
,并finally
处理似乎永远火,并调用done();
因此,规范超时并失败(请参阅Chrome Developer Console的屏幕截图)。
Jasmine套件和规格
describe("promise suite", function () {
var someMethod = function() {
var deferred = $q.defer();
setTimeout(function() {
console.log('All done calling resolve');
deferred.resolve('all done');
}, 500);
return deferred.promise;
}
iit("promise spec", function (done) {
someMethod()
.then(function(message) {
console.log('promise method resolved, running expectation');
expect(message).toBe('all done');
done();
})
.catch(function(error) {
console.log('promise was rejected ', error);
done();
})
.finally(function() {
console.log('calling done from finally');
done();
});
});
});
$ q promises在下一个摘要中得到解决...所以你需要触发一个摘要:
$rootScope.$digest();
尝试使用plunkr中的代码并且它正常工作,请参阅控制台输出。
由于示例中没有app引导程序,我使用https://docs.angularjs.org/api/ng/function/angular.injector和ng
模块来获取$q
服务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.