当我运行伊斯坦布尔的代码覆盖率时,我的代码覆盖率百分比被未经测试的方法降低了。当我去测试该方法时,我发现我已经对其进行过测试,并且与该文件中的其他测试相比,该测试用例相当大。
以下代码来自规格文件:
describe('getEdiRequestsListData', () => {
it('with customer id', () => {
let data: any;
let r = component.getEdiRequestsListData('', 1);
customerService.getEdiRequestsForAccount.and.returnValue(of(data));
component.getEdiRequestsListData('', 1);
expect(component.resubmitSelection).toEqual([]);
expect(component.cancelSelection).toEqual([]);
expect(component.cancelAllChecked).toEqual(false);
expect(component.resubmitAllChecked).toEqual(false);
expect(component.resubmitSelection).toEqual([]);
expect(component.totalRequestCount).toEqual(0);
});
it('with account id', () => {
let data: any;
let r = component.getEdiRequestsListData(null, null, 1);
customerService.getEdiRequestsForAccount.and.returnValue(of(data));
component.getEdiRequestsListData(null, null, 1);
expect(component.resubmitSelection).toEqual([]);
expect(component.cancelSelection).toEqual([]);
expect(component.cancelAllChecked).toEqual(false);
expect(component.resubmitAllChecked).toEqual(false);
expect(component.resubmitSelection).toEqual([]);
expect(component.totalRequestCount).toEqual(0);
});
it('without account id and customer id', () => {
let data: any;
let r = component.getEdiRequestsListData();
customerService.getEdiRequestsForAccount.and.returnValue(of(data));
component.getEdiRequestsListData();
expect(component.resubmitSelection).toEqual([]);
expect(component.cancelSelection).toEqual([]);
expect(component.cancelAllChecked).toEqual(false);
expect(component.resubmitAllChecked).toEqual(false);
expect(component.resubmitSelection).toEqual([]);
expect(component.totalRequestCount).toEqual(0);
});
});
以下代码是组件:
getEdiRequestsListData(orderBy? : string, customerId?: number, accountId?: number) {
var _self = this;
this.dataSource = new DataSource({
store: new CustomStore({
key: 'Id',
load: function (loadOptions: any) {
console.log('Load Options: ' + JSON.stringify(loadOptions));
var filters = _self.odataFilterService.buildFilters(loadOptions.filter);
_self.currentFilterString = _self.odataFilterService.buildFilterString(filters);
var sortBy = loadOptions.sort ? _self.odataFilterService.buildSortString(loadOptions.sort) : orderBy;
if(customerId) {
return _self.customersService.getEdiRequestsForCustomer(customerId, loadOptions.skip / (loadOptions.take || 20), (loadOptions.take || 20), sortBy, filters)
.toPromise()
.then((data: any) => {
_self.resubmitSelection = [];
_self.cancelSelection = [];
// _self.reviewSelection = [];
_self.resubmitAllChecked = false;
_self.cancelAllChecked = false;
_self.totalRequestCount = data && data.length ? data[0].TotalCount : 0;
_self.foundOneCancelItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 0; }) != null;
_self.foundOneResubmitItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 6; }) != null;
// _self.foundOneReviewItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 6; }) != null;
return {
data: data,
totalCount: _self.totalRequestCount
}
})
.catch(error => {
_self.alertifyService.error('Could not load edi requests');
});
} else if(accountId) {
return _self.customersService.getEdiRequestsForAccount(accountId, loadOptions.skip / (loadOptions.take || 20), (loadOptions.take || 20), sortBy, filters)
.toPromise()
.then((data: any) => {
_self.resubmitSelection = [];
_self.cancelSelection = [];
// _self.reviewSelection = [];
_self.resubmitAllChecked = false;
_self.cancelAllChecked = false;
_self.totalRequestCount = data && data.length ? data[0].TotalCount : 0;
_self.foundOneCancelItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 0; }) != null;
_self.foundOneResubmitItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 6; }) != null;
// _self.foundOneReviewItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 6; }) != null;
return {
data: data,
totalCount: _self.totalRequestCount
}
})
.catch(error => {
_self.alertifyService.error('Could not load edi requests');
});
} else {
return _self.customersService.getEdiRequestsForAccount(0, loadOptions.skip / (loadOptions.take || 20), (loadOptions.take || 20), sortBy, filters)
.toPromise()
.then((data: any) => {
_self.resubmitSelection = [];
_self.cancelSelection = [];
// _self.reviewSelection = [];
_self.resubmitAllChecked = false;
_self.cancelAllChecked = false;
_self.totalRequestCount = data && data.length ? data[0].TotalCount : 0;
_self.foundOneCancelItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 0; }) != null;
_self.foundOneResubmitItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 6; }) != null;
// _self.foundOneReviewItem = _.find(data, d => { return (d as EdiRequest).EditransactionStatus === 6; }) != null;
return {
data: data,
totalCount: _self.totalRequestCount
}
})
.catch(error => {
_self.alertifyService.error('Could not load edi requests');
});
}
}
})
})
return this.dataSource;
}
下图显示了覆盖率报告。红色突出显示标记未测试/未发现的代码行,但是正如您在我的规范中所看到的,我已经对这些进行了实际测试 https://imgur.com/a/5KipWb6