如何使用可观察的调用测试void方法

时间:2019-07-05 18:17:10

标签: angular testing jestjs

我有以下方法:

  public listenToVerificationResult(): void {
    const kycVerificationResultSubscription = this._kycFacade.getIsVerified.subscribe(
      result => {
        if (result !== undefined) {
          if (result === true) {
            this._router.navigate([RouteTypes.kycSuccess]);
          } else {
            this._router.navigate([RouteTypes.kycError]);
          }

          if (kycVerificationResultSubscription) {
            kycVerificationResultSubscription.unsubscribe();
            this._kycFacade.setIsVerified(undefined);
          }
        }
      },
    );
  }

我正在用fakeAsync测试它,但是由于某种原因,我无法收听路由器导航。我包含了RouterTestingModule并设置了路由器导航间谍:

let routerSpyNavigate: jasmine.Spy;

然后在beforeEach内部:

routerSpyNavigate = spyOn(
  TestBed.get(Router),
  'navigate',
).and.callThrough();

然后进行测试:

it('should redirect to success route if verification result is true', fakeAsync(() => {
  kycFacadeMock.setIsVerified(true);
  service.listenToVerificationResult();

  tick();

  let result;

  kycFacadeMock.getIsVerified.subscribe(_result => {
    result = _result;
  });

  tick();

  expect(result).toEqual(true);
  expect(routerSpyNavigate).toHaveBeenCalledWith([RouteTypes.kycSuccess]);
}));

不确定我在哪里弄错了。

0 个答案:

没有答案