【发布时间】:2021-09-16 07:20:06
【问题描述】:
我正在为我的 Angular 项目使用以下内容 角 CLI:10.2.3
节点:12.22.1
我有以下依赖项(用于测试)
"devDependencies": {
"@angular-devkit/build-angular": "^0.1002.0",
"@angular/cli": "^10.2.3",
"@angular/compiler-cli": "~10.0.2",
"@ngneat/spectator": "^5.13.0",
"@types/jest": "^26.0.23",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0-next.1",
"jest": "^27.0.5",
"jest-mock-extended": "^1.0.16",
"jest-preset-angular": "^9.0.4",
"ngx-deploy-npm": "^1.2.2",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~3.9.5"
}
我想了解如何提高我的测试覆盖率,以便 sonarqube 报告显示更好的覆盖率。
我有一个组件如下。我为构造函数添加了“istanbul ignore next”注释,否则覆盖率报告显示我没有覆盖那些代码行。我不想测试私有构造函数,所以使用上面的注释。
// Private class used by the component. do not want to test it.
export class Summary {
/* istanbul ignore next */
constructor(
public vDate: Date,
public bookName: string,
.
.
) {
}
}
export class SummaryComponent implements OnInit {
ngOnInit(): void {
.
.
}
handleUserSelection(selection) {
console.log("Handle User Selection");
console.log("Selection: " + selection.selectedDateTo.year);
console.log("Selection: " + selection.selectedDateFrom.month);
if (selection.selectedDateTo instanceof Date && selection.selectedDateTo.getTime()) {
this.currentDateTo = this.util.formatCalendarDateToDateStr(selection.selectedDateTo);
} else {
this.currentDateTo = this.util.formatCalendarDateToDateStr(new Date(selection.selectedDateTo.year,selection.selectedDateTo.month - 1,selection.selectedDateTo.day));
}
if (selection.selectedDateFrom instanceof Date && selection.selectedDateFrom.getTime()) {
this.currentDateFrom = this.util.formatCalendarDateToDateStr(selection.selectedDateFrom);
} else {
this.currentDateFrom = this.util.formatCalendarDateToDateStr(new Date(selection.selectedDateFrom.year,selection.selectedDateFrom.month - 1,selection.selectedDateFrom.day));
}
console.log(selection);
.
.
}
}
基本上,我在组件中有一个方法。我的测试文件如下所示:
describe('SummaryComponent', () => {
let spectator: Spectator<SummaryComponent>;
const createComponent = createComponentFactory({
component: SummaryComponent,
declarations: [],
imports: [HttpClientTestingModule,
MatSnackBarModule,
RouterTestingModule
],
providers: [
{ provide: MatDialog, useClass: MatDialogMock },
],
schemas: [NO_ERRORS_SCHEMA],
mocks: [BasicAuthService,
SummaryDataService,
],
detectChanges: false
});
beforeEach(()=> {
spectator= createComponent();
});
it('should handle user selection', () => {
jest.spyOn(spectator.component, 'handleUserSelection').mockReturnValue(null);
spectator.component.handleUserSelection(selection);
expect(spectator.component.handleUserSelection).toHaveBeenCalled();
});
});
我怎样才能改进测试,以便覆盖该方法?
更新:
我尝试模拟实现如下功能:
it('should handle user selection', () => {
jest.spyOn(spectator.component, 'handleUserSelection').mockImplementation(() => {
let jSelection: any = {
selectedDateFrom: {year: 2020, month: 5, day: 29},
selectedDateTo: {year: 2020, month: 6, day: 30},
};
console.log("Handle User Selection");
console.log("Selection: " + jSelection.selectedDateTo.year);
console.log("Selection: " + jSelection.selectedDateFrom.month);
if (jSelection.selectedDateTo instanceof Date && jSelection.selectedDateTo.getTime()) {
spectator.component.currentDateTo = spectator.component.util.formatCalendarDateToDateStr(jSelection.selectedDateTo);
} else {
spectator.component.currentDateTo = spectator.component.util.formatCalendarDateToDateStr(new Date(jSelection.selectedDateTo.year,jSelection.selectedDateTo.month - 1,jSelection.selectedDateTo.day));
}
if (jSelection.selectedDateFrom instanceof Date && jSelection.selectedDateFrom.getTime()) {
spectator.component.currentDateFrom = spectator.component.util.formatCalendarDateToDateStr(jSelection.selectedDateFrom);
} else {
spectator.component.currentDateFrom = spectator.component.util.formatCalendarDateToDateStr(new Date(jSelection.selectedDateFrom.year,jSelection.selectedDateFrom.month - 1,jSelection.selectedDateFrom.day));
}
console.log("Handle user selection Done!");
});
spectator.component.handleUserSelection(selection);
expect(spectator.component.handleUserSelection).toHaveBeenCalled();
});
覆盖率仍然显示相同,即根本没有覆盖该功能?
我应该怎么做才能覆盖该方法?
【问题讨论】:
标签: angular sonarqube code-coverage istanbul ts-jest