【发布时间】:2020-01-03 06:12:46
【问题描述】:
运行代码覆盖率测试时,它说函数 clearAllValues 没有在测试中执行,但我在下面的测试中调用了这个函数
测试
it('clearAllValues should be called by click', () => {
spyOn(component, 'clearAllValues');
const button = fixture.debugElement.query(By.css('.clearAllValuesBtn')).nativeElement;
button.click();
fixture.whenStable().then(() => {
expect(component.clearAllValues).toHaveBeenCalled();
expect(component.value).toEqual('');
expect(component.allowReset).toEqual(false);
expect(component.term).toEqual('');
});
});
it('should call clearAllValues function', () => {
spyOn(component, 'clearAllValues');
component.clearAllValues();
expect(component.clearAllValues).toHaveBeenCalled();
});
组件
clearAllValues = () => {
this.value = '';
this.allowReset = false;
this.term = '';
}
【问题讨论】:
-
您在
expecttations 之前尝试过fixture.detectChanges();吗?
标签: angular jasmine karma-jasmine karma-coverage