【发布时间】:2021-10-08 14:12:01
【问题描述】:
我有一个<a /> 标签,如下所示:
<a class="md-color--blue-50 md-link" (click)="resetdevicesearch()" >Reset filters</a>
在我的ts 文件中,函数是这样定义的:
resetdevicesearch() {
const defaultsearchvalue = {value: {name: 'Any Name/Address', id: 1}};
this.filterdeviceinfodata = {RisClass: '', Id: '', Name: ''};
this.deviceonlyclicked = false;
this.initform();
this.devicesearch = {name: 'Phone', initial: 'Phone', id: 1};
this.deviceStatus = this.devicesearchjson.PhoneDevice.DeviceStatus.devicestatusKey;
this.updatestatus(this.devicesearchjson.PhoneDevice.DeviceStatus.defaultKey);
this.updatedownloadstatus();
this.updateProtocol();
this.devicecheckboxvalue = [];
this.selecttdefaultcheckbox(this.devicesearchjson.PhoneDevice.Monitorfollowingattribute.monitorfollowingattributeKey);
this.enableinputfield(defaultsearchvalue);
this.inputchange = {name: 'Any Name/Address', id: 1, label: ''};
// @ts-ignore
this.filterdeviceinfodata = {RisClass: '', Id: '', Name: ''};
}
现在,我正在尝试编写一个Unit Test 来调用该函数。这是测试套件的样子:
it('should get the anchor tag', () => {
fakeAsync(() => {
spyOn(component, 'resetdevicesearch');
fixture.detectChanges();
const input: ElementRef = fixture.debugElement.query(By.css('.md-link'));
input.nativeElement.blur();
tick();
expect(page).toBeTruthy();
expect(page.anchorTag).toBeTruthy();
expect(component.resetdevicesearch).toHaveBeenCalled();
});
})
现在,测试运行成功,但是当我检查覆盖文件夹时,它显示Function not covered
为什么会这样?
编辑:
修改代码:
it('should get the anchor tag', () => {
fakeAsync(() => {
jest.spyOn(component, 'resetdevicesearch');
fixture.detectChanges();
const input: ElementRef = fixture.debugElement.query(By.css('.md-link'));
input.nativeElement.blur();
tick();
expect(page).toBeTruthy();
expect(page.anchorTag).toBeTruthy();
expect(component.resetdevicesearch).toHaveBeenCalled();
});
})
【问题讨论】: