简体   繁体   中英

RxJS marble test fails when using my own observable instead of creating one with the createHotObservable method

I am trying to create a simple test with RxJS marbles.

I am using mocha and chai.

I am instantiating a new test scheduler and I do not want to use the "testScheduler.createHotObservable method" because I want to use my own Observable, the "Observable.of(4)"

 const testScheduler = new TestScheduler(assert.deepEqual.bind(assert));

      const expected = "a";
      const expectedStateMap = {
        a: 4
      };

      testScheduler.expectObservable(Observable.of(4)).toBe(expected, expectedStateMap);

      testScheduler.flush();

This is the error:

 AssertionError: expected [ Array(2) ] to deeply equal [ Array(1) ]
      + expected - actual

           "notification": {
             "error": [undefined]
             "hasValue": true
             "kind": "N"
      -      "value": 4
      +      "value": "4"
           }
         }
      -  {
      -    "frame": 0
      -    "notification": {
      -      "error": [undefined]
      -      "hasValue": false
      -      "kind": "C"
      -      "value": [undefined]
      -    }
      -  }
       ]

      at TestScheduler.flush (node_modules/rxjs/src/testing/TestScheduler.ts:135:12)

Any ideas what is wrong?

Yes, it doesn't work. What hot , cold observable creation method does is, creating observable based on given marble and setup internally inside of testscheduler. When testscheduler executes via flush , it iterates all observable and flush. If custom observable is provided, testscheduler doesn't know about existence of those observable and not flush those.

It is just limitation of current testscheduler implementation - unless monkey patch testscheduler to accept custom observable it may not work as expected.

You just forgot to complete your Observable.

  const expected = "a";
  const expectedStateMap = {
    a: 4
  };

can be refactored to

  const expected = "4|";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM