如何使用大理石测试switchMap内的代码

时间:2019-10-29 12:14:53

标签: angular unit-testing karma-jasmine jasmine-marbles

我的服务方法如下:

myMethod(): Observable<boolean> {
  return this.anotherService.method().pipe(
    switchMap(() => {
      this.anotherService.anotherMethod();
      return of(true);
    })
  );
}

我已经使用大理石创建了单元测试,以测试myMethod返回的是(true),并且可以正常工作:

it('should return of(true)', () => {
  anotherService.method.and.returnValue(
      cold('3ms (a|)', {
        a: {response: 'success'}
      })
    );
  anotherServiceSpy.method.calls.reset();
  anotherService.anotherMethod.calls.reset();
  const scheduler: TestScheduler = new TestScheduler((actual, expected) => {
    expect(actual).toEqual(expected);
  });
  scheduler.run(({ expectObservable }) => {
    expectObservable(service.myMethod()).toBe('3ms (a|)', { a: true });
    expect(anotherService.method).toHaveBeenCalledTimes(1);
  });
});

但是我还想测试是否还调用了anotherService.anotherMethod。我该怎么办?

0 个答案:

没有答案