单元测试 AngularJS 服务方法使用 $q.all 解决多个承诺
Unit testing AngularJS service method thet resolves multiple promises with $q.all
我正在尝试测试以下服务方法,该方法调用另一个方法以使用不同参数多次检索数据。
我一次只测试过解决一个承诺,所以我不确定如何将此方法作为 karma/jasmine 单元测试来处理:
getMappings(dealerId){
let dfd = this.$q.defer();
let promises = [];
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.DEALER, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.DAYCOUNT, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.FREQUENCY, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.RATE_STATIC, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.SETTLEMENT_TYPE, dealerId));
this.$q.all(promises)
.then((resp) => {
dfd.resolve({
dealers: resp[0].data,
daycounts: resp[1].data,
frequencies: resp[2].data,
rateStatics: resp[3].data,
settlementTypes: resp[4].data
});
}, (resp) => {
dfd.reject(resp);
});
return dfd.promise;
}
如有任何帮助,我们将不胜感激。谢谢:)
it('should get the mappings for listing', () => {
let dealerId = 1;
let resp;
let count = 0;
let dealers = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 2,
"description": "Wayne Enterprise"
},
"synonym": "CTRPTYCODE 1"
}
];
let daycount = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "DAYCT 1"
},
"synonym": "DAYCTCODE 1"
}
];
let frequency = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "FREQ A"
},
"synonym": "FREQCODE 1"
}
];
let ratestat = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "RATESTAT 1"
},
"synonym": "RATESTATCODE 1"
}
];
let settlement = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "SETTYPE A"
},
"synonym": "SETTYPECODE 1",
"$$hashKey": "object:221"
}
];
spyOn(service, 'getMappings').and.callThrough();
spyOn(DataService, 'getTranslationMappings').and.callFake(function(){
var defer = this.$q.defer();
let data;
switch(count) {
case 0:
data = dealers;
break;
case 1:
data = daycount;
break;
case 2:
data = frequency;
break;
case 3:
data = ratestat;
break;
case 4:
data = settlement;
break;
}
defer.resolve({
success: true,
error: null,
data: data
});
count++;
return defer.promise;
});
service.getMappings(dealerId).then((data) => {
resp = data;
});
scope.$digest();
expect(service.getMappings).toHaveBeenCalled();
expect(service.getMappings).toHaveBeenCalledWith(dealerId);
expect(DataService.getTranslationMappings).toHaveBeenCalled();
expect(DataService.getTranslationMappings.calls.count()).toEqual(5);
expect(DataService.getTranslationMappings.calls.argsFor(0)).toEqual(['DEALER', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(1)).toEqual(['DAYCOUNT', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(2)).toEqual(['FREQUENCY', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(3)).toEqual(['RATE_STATIC', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(4)).toEqual(['SETTLEMENT_TYPE', dealerId]);
expect(resp.dealers).toEqual(dealers);
expect(resp.daycounts).toEqual(daycount);
expect(resp.frequencies).toEqual(frequency);
expect(resp.rateStatics).toEqual(ratestat);
expect(resp.settlementTypes).toEqual(settlement);
});
已解决! :)
我正在尝试测试以下服务方法,该方法调用另一个方法以使用不同参数多次检索数据。
我一次只测试过解决一个承诺,所以我不确定如何将此方法作为 karma/jasmine 单元测试来处理:
getMappings(dealerId){
let dfd = this.$q.defer();
let promises = [];
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.DEALER, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.DAYCOUNT, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.FREQUENCY, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.RATE_STATIC, dealerId));
promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.SETTLEMENT_TYPE, dealerId));
this.$q.all(promises)
.then((resp) => {
dfd.resolve({
dealers: resp[0].data,
daycounts: resp[1].data,
frequencies: resp[2].data,
rateStatics: resp[3].data,
settlementTypes: resp[4].data
});
}, (resp) => {
dfd.reject(resp);
});
return dfd.promise;
}
如有任何帮助,我们将不胜感激。谢谢:)
it('should get the mappings for listing', () => {
let dealerId = 1;
let resp;
let count = 0;
let dealers = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 2,
"description": "Wayne Enterprise"
},
"synonym": "CTRPTYCODE 1"
}
];
let daycount = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "DAYCT 1"
},
"synonym": "DAYCTCODE 1"
}
];
let frequency = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "FREQ A"
},
"synonym": "FREQCODE 1"
}
];
let ratestat = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "RATESTAT 1"
},
"synonym": "RATESTATCODE 1"
}
];
let settlement = [
{
"id": 1,
"dealerId": 1,
"translation": {
"id": 1,
"description": "SETTYPE A"
},
"synonym": "SETTYPECODE 1",
"$$hashKey": "object:221"
}
];
spyOn(service, 'getMappings').and.callThrough();
spyOn(DataService, 'getTranslationMappings').and.callFake(function(){
var defer = this.$q.defer();
let data;
switch(count) {
case 0:
data = dealers;
break;
case 1:
data = daycount;
break;
case 2:
data = frequency;
break;
case 3:
data = ratestat;
break;
case 4:
data = settlement;
break;
}
defer.resolve({
success: true,
error: null,
data: data
});
count++;
return defer.promise;
});
service.getMappings(dealerId).then((data) => {
resp = data;
});
scope.$digest();
expect(service.getMappings).toHaveBeenCalled();
expect(service.getMappings).toHaveBeenCalledWith(dealerId);
expect(DataService.getTranslationMappings).toHaveBeenCalled();
expect(DataService.getTranslationMappings.calls.count()).toEqual(5);
expect(DataService.getTranslationMappings.calls.argsFor(0)).toEqual(['DEALER', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(1)).toEqual(['DAYCOUNT', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(2)).toEqual(['FREQUENCY', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(3)).toEqual(['RATE_STATIC', dealerId]);
expect(DataService.getTranslationMappings.calls.argsFor(4)).toEqual(['SETTLEMENT_TYPE', dealerId]);
expect(resp.dealers).toEqual(dealers);
expect(resp.daycounts).toEqual(daycount);
expect(resp.frequencies).toEqual(frequency);
expect(resp.rateStatics).toEqual(ratestat);
expect(resp.settlementTypes).toEqual(settlement);
});
已解决! :)