Jest RXjs6 Marbles,如何测试现有的可观察物

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

我正在尝试找出如何为以下内容构建一个笑话单元测试:

我有很多现有的可观测对象和我想在项目中测试的主题。因此,我开始使用在jest内使用RXjs大理石创建的简单测试用例进行了假设:

      const testScheduler = new TestScheduler((actual, expected) => {
        expect(actual).toEqual(expected);
      });

      testScheduler.run(({cold, expectObservable}) => {
        const inputSubject = new Subject();
        const outputSubject = new Subject();
        inputSubject.pipe(map(val => val * 2)).subscribe(outputSubject);

        cold('a|', {a:1}).subscribe(inputSubject);

        expectObservable(outputSubject).toBe('a|', {a:2});
      });
    }

虽然此测试确实运行,但失败。我无法理解测试运行的输出。我想知道我是否做错了什么,或者我是否误解了一般情况。也许只是对输出进行一些解释会有所帮助。

    - Expected
    + Received

    @@ -1,16 +1,7 @@
      Array [
        Object {
    -     "frame": 0,
    -     "notification": Notification {
    -       "error": undefined,
    -       "hasValue": true,
    -       "kind": "N",
    -       "value": 2,
    -     },
    -   },
    -   Object {
          "frame": 1,
          "notification": Notification {
            "error": undefined,
            "hasValue": false,
            "kind": "C",

...... the code ......

      at TestScheduler.assertDeepEqual (common/chat/newMessages/ratifiedSendMessages/twilioRatifiedSendMessages.unit.test.js:27:24)
      at node_modules/rxjs/src/internal/testing/TestScheduler.ts:159:14
          at Array.filter (<anonymous>)
      at TestScheduler.Object.<anonymous>.TestScheduler.flush (node_modules/rxjs/src/internal/testing/TestScheduler.ts:157:39)
      at TestScheduler.run (node_modules/rxjs/src/internal/testing/TestScheduler.ts:392:12)
      at Object.<anonymous> (common/chat/newMessages/ratifiedSendMessages/twilioRatifiedSendMessages.unit.test.js:30:21)
jestjs rxjs6 rxjs-marbles
1个回答
0
投票

NVM。看起来我应该使用“热”而不是“冷”。当我将“冷”呼叫改为“热”时,它工作正常。尽管如果还有其他人想解释这是为什么,那么我全是耳目一新。

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