首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为可观察到的combineLatest编写大理石测试?

如何为可观察到的combineLatest编写大理石测试?
EN

Stack Overflow用户
提问于 2022-11-14 12:56:46
回答 1查看 21关注 0票数 0

我有一个可观察的,它需要两个Observable<boolean>,并使用combineLatest对它们运行一个“或”操作。

代码语言:javascript
复制
interface LoadingEventEmitter {
  isLoading$: Observable<boolean>
}

export class LoadingService {
  isLoading$: Observable<boolean>;

  constructor(
    private requests: LoadingEventEmitter,
    private lazyRoutes: LoadingEventEmitter
  ) {
    this.isLoading$ = combineLatest([
      this.requests.isLoading$,
      this.lazyRoutes.isLoading$,
    ]).pipe(
      map(
        ([requestsLoading, lazyRoutesLoading]) =>
          requestsLoading || lazyRoutesLoading
      )
    );
  }
}

我正在尝试用jasmine-marbles测试它。

代码语言:javascript
复制
fdescribe('LoadingService', () => {
  const createLoadingService = (
    requests$: Observable<boolean>,
    lazyLoading$: Observable<boolean>
  ) => {
    const mockRequests = { isLoading$: requests$ };
    const mockLazyLoading = { isLoading$: lazyLoading$ };

    return new LoadingService(mockRequests, mockLazyLoading);
  };

  const values = { t: true, f: false };

  it('isLoading$ should be true when at least one of the sources emits true', () => {
    const a = cold('---t---f---f---', values); // First source
    const b = cold('f----t---------', values); // Second source
    const c = cold('---t-t-t---t---', values); // Result

    const service = createLoadingService(a, b);

    expect(service.isLoading$).toBeObservable(c);
  });
});

在我看来,这个测试很好,但是它失败了,出现了以下错误:

代码语言:javascript
复制
Expected $[0].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).
Expected $[1].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).
Expected $[2].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).
Expected $[3].notification to be a kind of Notification, but was Object({ kind: 'N', value: true, error: undefined }).

这个错误意味着什么?我如何修复它?

EN

回答 1

Stack Overflow用户

发布于 2022-11-14 14:13:27

我成功地用rxjs-marbles做了

代码语言:javascript
复制
  it(
    'isLoading$ should be true when at least one of the sources emits true',
    marbles((m) => {
      const a = m.cold('---t---f---f---', values);
      const b = m.cold('f----t---------', values);
      const c = '     ---t-t-t---t---';

      const service = createLoadingService(a, b);

      m.expect(service.isLoading$).toBeObservable(c, values);
    })
  );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74432000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档