我对这项工作和编程很陌生,我的任务是进行单元测试,并获得80 %的代码覆盖率。我无法获得此方法的代码覆盖率。它说声明没有包括在内。我做错了什么?
support.component.ts
searchId: string = '';
orchestration: string = '';
public clearText(){
this.searchId = '';
this.orchestration = '';
}support.component.html
<mat-select id="searchselected" (selectionChange)="clearText()">
<input type="text" class="form-control" id="searchId" [(ngModel)]=""searchId size="40" placeholder="enter value">
<input type="text" class="form-control" id="orchestration" [(ngModel)]=""searchId size="40" placeholder="enter orchestration">support.component.spec.ts
const spy = spy = spyon(component, 'clearText').and.callThrough();
component.searchId;
component.orchestration;
component.clearText();
expect(spy).toHaveBeenCalled();
expect(component.searchId).toEqual('');
expect(component.orechestion).toEqual('');发布于 2021-01-26 06:02:15
您不需要spyOn组件方法。外部方法(如服务方法)被监视,以避免在测试组件的方法时调用它们。
因此,您可以在这里直接调用该方法并测试以下条件:
support.component.spec.ts
component.clearText();
expect(component.searchId).toEqual('');
expect(component.orechestion).toEqual('');https://stackoverflow.com/questions/65896157
复制相似问题