angularjs - jasmine 测试用例,用于测试调用异步调用的函数
问题描述
var count = 0;
function abc(){
asyncService.getCount(function(response){
count = response.data;
}, function(error){
console.log(error);
});
}
it('check count', function(){
asyncService.getCount.and.returnValue(Promise.resolve(2));
ctrl.abc();
expect(count).toBe(2);
}
我想测试茉莉花中的计数值。请帮我解决一下这个。我找不到任何解决方案。我也嘲笑了这项服务。但它仍然给计数为 0
解决方案
使用 $q,您可以使用 $rootScope.$digest(); 自己解决它。
it('check count', function(){
asyncService.getCount.and.returnValue($q.resolve(2));
ctrl.abc();
$rootScope.$digest();
expect(count).toBe(2);
}
$q 是 Promise 的 AngularJS 包装器。
推荐阅读
- r - 从可编辑的数据集 rshiny rhandsontable 绘制图
- typescript - 如何检查泛型类型是否在打字稿中按名称具有属性?
- r - 如何按顺序命名列表的每个元素和子元素?
- java - 在docker上运行的mysql容器无法连接spring boot java api服务
- python - Python 遍历 df 并参考下一行
- python - 在 DataFrame 中删除重复项,其中列相同且时间戳接近
- python - 请在执行时帮助解决此错误
- oracle - 有没有办法从 WebSphere cast iron.. 调用 pl/sql 打包过程?
- ansible - 如何在 ansible playbook 中为每个模块使用不同的主机组
- jquery - 使用 grails 进行 Jquery 分页