Angularjs jasmine testcase用于测试调用异步调用的函数
我想测试茉莉花的计数值。请帮我做这个。我找不到任何解决办法。Angularjs jasmine testcase用于测试调用异步调用的函数,angularjs,jasmine,Angularjs,Jasmine,我想测试茉莉花的计数值。请帮我做这个。我找不到任何解决办法。 我也嘲笑这项服务。但它仍然将计数设为0使用$q,您可以使用$rootScope.$digest();;自己解决它 $q是Promise的AngularJS包装 将spyOn与异步服务一起使用 响应变量 正在传递给异步服务的回调函数中设置计数。该异步服务的返回值似乎是 微不足道的因此,您必须使用callFake 并执行回调 JSFiddle: 注意:您必须使用Jasmine版本2或更高版本才能使用上述代码段 var count
我也嘲笑这项服务。但它仍然将计数设为0使用$q,您可以使用$rootScope.$digest();;自己解决它 $q是Promise的AngularJS包装
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);
}
it('check count', function(){
asyncService.getCount.and.returnValue($q.resolve(2));
ctrl.abc();
$rootScope.$digest();
expect(count).toBe(2);
}
describe('testing', function(){
it('testing async service', function(){
spyOn(asyncService, 'getCount').and.callFake(function(res, err){
res({data: 2})
})
abc();
expect(count).toBe(2);
})
})