我有此代码:
public ngOnInit(): void {
this.searchControl.valueChanges.pipe(debounceTime(300), this.takeUntilDestroyed()).subscribe(query => {
this.officeService.searchQuery = query;
});
}
如何覆盖searchQuery设置器?
我写了通过谁的测试,但没有通过覆盖设置程序。
我的测试:
it('should searchQuery called ', () => {
const { component, service } = setup();
const query: string = 'test';
const fixture: ComponentFixture<SearchComponent> = TestBed.createComponent(SearchComponent);
const input = fixture.debugElement.query(By.css('input'));
const inputEvent = new Event('input');
const spiez: jasmine.Spy = spyOnProperty(service, 'searchQuery', 'set');
component.searchControl.valueChanges.subscribe((tmp: string) => expect(spiez).toHaveBeenCalledWith(tmp));
input.nativeElement.value = 'newString';
input.nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();
});
我用fakeAsync和tick进行的测试:
const { component, service } = setup();
const query: string = 'test';
const fixture: ComponentFixture<SearchComponent> = TestBed.createComponent(SearchComponent);
const input = fixture.debugElement.query(By.css('input'));
const inputEvent = new Event('input');
const spiez: jasmine.Spy = spyOnProperty(service, 'searchQuery', 'set');
component.searchControl.valueChanges.subscribe((tmp: string) => expect(spiez).toHaveBeenCalledWith(tmp));
input.nativeElement.value = 'newString';
input.nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();
tick(300);
}));
您应该这样做: