Javascript 在angular js中服务的单元测试承诺
我正在尝试对以下功能进行单元测试:Javascript 在angular js中服务的单元测试承诺,javascript,angularjs,unit-testing,Javascript,Angularjs,Unit Testing,我正在尝试对以下功能进行单元测试: Service.test=功能(id){ 返回此.getDetails(id).then(函数)(详细信息){ 返回“名称”; }); }; 此行将在spy上下文中执行getDetails的实际实现。你可以用像 spyOn(TemplateService, 'getDetails').and.returnValue(true) 相反。如果要测试回调内部发生的情况,通常需要通过$scope.$apply()手动触发摘要周期更新 此行将在spy上下文中执行g
Service.test=功能(id){
返回此.getDetails(id).then(函数)(详细信息){
返回“名称”;
});
};
此行将在spy上下文中执行getDetails的实际实现。你可以用像
spyOn(TemplateService, 'getDetails').and.returnValue(true)
相反。如果要测试回调内部发生的情况,通常需要通过$scope.$apply()手动触发摘要周期更新
此行将在spy上下文中执行getDetails的实际实现。你可以用像
spyOn(TemplateService, 'getDetails').and.returnValue(true)
相反。如果要测试回调内部发生的情况,通常需要通过$scope.$apply()手动触发摘要周期更新 试试这样做
spyOn(TemplateService, 'getDetails').and.returnValue($q.when('mockDetails'));
$rootScope.$digest();
这将允许您进行如下测试:
it('should be in pending state', function(done) {
spyOn(TemplateService, 'getDetails').and.returnValue($q.when('mockDetails'));
TemplateService.test().then(function(result) {
expect(result).toBe('name'); // this could also be a value computed with "mockDetails"
done();
});
$rootScope.$digest();
});
试着做这样的事情
spyOn(TemplateService, 'getDetails').and.returnValue($q.when('mockDetails'));
$rootScope.$digest();
这将允许您进行如下测试:
it('should be in pending state', function(done) {
spyOn(TemplateService, 'getDetails').and.returnValue($q.when('mockDetails'));
TemplateService.test().then(function(result) {
expect(result).toBe('name'); // this could also be a value computed with "mockDetails"
done();
});
$rootScope.$digest();
});
我试过了,成功了!!谢谢:)如果我有任何问题,我会打扰你的,好吗?我试过了,它起作用了!!谢谢:)如果我有问题,我会问你更多的问题,好吗?