如何在订阅中测试二传手?角

问题描述 投票:0回答:1

我有此代码:

    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();
  });

我用fakeAsynctick进行的测试:

    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);
  }));
angular testing jasmine setter subscribe
1个回答
0
投票

您应该这样做:

© www.soinside.com 2019 - 2024. All rights reserved.