我发现几个职位的测试服务,说明此代码的方式来进行异步单元测试:AngularJS,摩卡,柴:有承诺
服务:
angular.module('miservices', [])
.service('myAppServices', ['$http', 'httpcalls', function($http, httpcalls) {
this.getAccountType = function(){
return httpcalls.doGet('http://localhost:3010/...').then(function(data){
return data;
}, function(error){
...
});
};
...
测试:
describe('testing myAppServices', function(){
beforeEach(module('smsApp'));
it('should handle names correctly', inject(function(myAppServices){
myAppServices.getAccountType()
.then(function(data) {
expect(data).equal({...});
});
...
我们使用的是AngularJS,Mocha,Chai,我们安装了Sinon。
测试永远不会到达.then
部分,但为什么?
谢谢!
这个问题不是承诺,而是$ http。你必须嘲笑请求,你不这样做。 – estus
你的意思是httpBackend他们? – Ramon
没错。如果doGet和getAccountType对实际的http响应没有任何影响,那么您可以跳过此规范,并为其他规范模拟getAccountType。 – estus