【发布时间】:2020-11-20 15:28:25
【问题描述】:
我正在尝试编写一个测试路径,该路径存在于 ngOnInit 函数中。而且我认为我已经找到了查看stackoverflow的方法。函数代码为:
ngOnInit() {
this.route.paramMap.subscribe((params: ParamMap) => {
this.partNoKey = params.get('partNoKey');
this.from = params.get('from');
// console.log(this.partNoKey);
// console.log(this.from);
if(this.from=="ncCutterSheetEdit"){
this.fromCutterSheet=true;
}
});
我目前的测试用例是:
it('should set fromCutterSheet to true when from is ncCutterSheetEdit', () =>{
component.from = "ncCutterSheetEdit";
fixture.detectChanges();
expect(component.from).toContain('ncCutterSheetEdit');
expect(component.fromCutterSheet).toBeTrue();
})
如果我注释掉最后一个期望语句,则测试通过,但是当我取消注释该期望语句时,它会失败并出现错误“预期为真为假”。
在编写测试用例后,当我查看覆盖率报告时,它仍将 if 语句显示为未采用的路径。我猜我没有合适的例子来说明我想做的事情。
谁能指点我这个测试用例的更合适的例子。
【问题讨论】:
标签: javascript angular typescript jasmine karma-jasmine