我有一個函式,并且單元測驗的覆寫率為 75%,但是,我希望單元測驗的覆寫率為 100%。這是函式:
calculateRatingSummary(): void {
if (this.averageRating > 0) {
this.avgRatings = Math.trunc(this.averageRating);
this.avgRatingCollection.length = this.avgRatings;
this.noRatings = this.totalRating - this.avgRatings;
if (this.averageRating % 1 > 0) {
this.noRatings--;
}
this.noRatingsCollection.length = this.noRatings;
} else {
this.noRatingsCollection.length = this.totalRating;
}
}
這是我為此方法撰寫的所有單元測驗:
describe('calculateRatingSummary', () => {
it('check if company has peer review average rating', () => {
component.averageRating = 2.3;
component.calculateRatingSummary();
expect(component.avgRatingCollection).not.toBeFalse();
});
it('rating display with no decimals if it has or not', () => {
component.averageRating = 3.6;
component.calculateRatingSummary();
expect(component.avgRatings).toEqual(3);
});
it('all rating stars displays as light shaded when averageRating is smaller than zero', () => {
component.averageRating = 0;
component.calculateRatingSummary();
expect(component.noRatingsCollection.length).toEqual(5);
});
it('if averageRating has decimals, noRatings is noRatings minus 1', () => {
component.averageRating = 4.1;
component.calculateRatingSummary();
expect(component.noRatings).toEqual(component.noRatings);
});
});
但是如果在運行時進行測驗,我將無法獲得第一個嵌套 npm run test-cover && npm run cover-report
我得到 100% 的通過測驗,但覆寫了 75% 的分支,因為它說測驗沒有覆寫:

這個條件:
if (this.averageRating % 1 > 0) {
this.noRatings--;
}
沒有被測驗覆寫,我想知道為什么?無論我嘗試什么,我都無法對其進行測驗。你能幫助我嗎?
uj5u.com熱心網友回復:
I think else condition is not met in any specs you are expecting, you can try this it might work as our motive is to satisy the else condition i.e.. this.averageRating % 1 < 0
Example spec -
it('if averageRating modulos 1 equal to less than 0', () => {
component.averageRating = 2;
component.calculateRatingSummary();
expect(component).toBeTruthy();
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/336051.html
上一篇:Python模塊沖突(tkinter和pylab)導致PycharmPython控制臺出現奇怪的不一致
下一篇:Mockito-模擬自動連線服務
