我是 Jest 的新手,希望有人能帮助我。
我目前已将 Jest 集成到我的 Angular 应用程序中,并正在努力提高我的单元测试覆盖率。
我发现我发现了线条。我一直在尝试编写一个测试来覆盖第 16 行 - this.hidePlaceHolder(),但它一直显示为未覆盖。
@HostListener('ngModelChange') hidePlaceHolderOnChange() {
this.hidePlaceHolder();
}
describe('HidePlaceholderDirective', () => {
// const ren: Renderer2;
// const elem: ElementRef;
let directive;
let directiveEl;
let fixture: ComponentFixture<DirectiveTestComponent>;
let component;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [HidePlaceholderDirective, DirectiveTestComponent],
}).compileComponents();
fixture = TestBed.createComponent(DirectiveTestComponent);
component = fixture.componentInstance;
directiveEl = fixture.debugElement.query(By.directive(HidePlaceholderDirective));
directive = directiveEl.injector.get(HidePlaceholderDirective);
});
it('should create an instance', () => {
expect(directive).toBeTruthy();
});
it('should call hidePlaceholder', () => {
directiveEl.dispatchEvent(new Event('input'));
expect(directive.hidePlaceHolder).toHaveBeenCalled();
});
});