如何测试订阅块内部主题的发射?

问题描述

我正在尝试学习rx大理石进行单元测试。我在测试subscribe块内的behaviorsubject的值时遇到问题。主题车也有问题。

  private readonly caRSSubject = new Subject();
  readonly cars$ = this.caRSSubject.asObservable();
  private readonly carsLoadingSubject = new BehaviorSubject<boolean>(false);
  readonly carsLoading$ = this.carsLoadingSubject.asObservable();

  constructor(private carsBackendService: CarsBackendService) { }

  public loadCars(carsPayload: Car): void {
    this.carsLoadingSubject.next(true);
    this.carsBackendService.getCars(carsPayload)
      .subscribe(
      cars => {
        this.caRSSubject.next(cars);
        this.carsLoadingSubject.next(false);
      },err => {
        console.log('Error while getting cars',err);
        this.carsLoadingSubject.next(false);
        }
      );
  }

我为此代码块编写了以下测试:

  it('should carsLoading$ return true when loadCars started and false after loded',() => {
    scheduler.run(({ expectObservable,flush }) => {
      service.loadCars(mockCarPayload);
      flush();
      expectObservable(service.carsLoading$).toBe('a-b-c',{a: false,b: true,c: false });
    });
  });

问题是我收到错误消息,即carsLoading $发出的项目的预期长度应为1,但我希望为3,且值始终为false。我应该如何测试?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)