如何在 Angular 中使用 Karma-Jasmine 对嵌套的 if 条件进行单元测试?
How to unit test a nested if condition in with Karma-Jasmine in Angular?
我有一个函数,单元测试的覆盖率为 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--;
}
没有被测试覆盖,我想知道为什么?无论我尝试什么,我都无法对其进行测试。你能帮帮我吗?
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();
});
我有一个函数,单元测试的覆盖率为 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--;
}
没有被测试覆盖,我想知道为什么?无论我尝试什么,我都无法对其进行测试。你能帮帮我吗?
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();
});