简体   繁体   中英

Rxjs marble testing

Trying to understand what is the best way to unit test RXjs 5 subscription. So for example, if I have a complex observables chain and some business logic inside the subscribe block, how should I test it? the options I see are:

  1. Use Test scheduler with advanceBy operators (similar to how you would do it in rxjs 4)
  2. Marble testing, but as far as i understand marble testing only cover the observable so you can test what is going to be the output of the observable chain according to a specific input, but this test doesnt cover the subscription block...

any advice or ideas will be very helpful

If you assert that the observable returned from your chain matches your marble diagram you are also testing that your observer methods are called correctly. If you want to test your observer methods I would do that independently.

With marble testing you have the possibility to test if your Observable has a subscription. Let's say you have for example the following source observable.

const source =  m.hot("--^-a-b-c-|");

You could then test if your observable has the correct subscription:

const expectedSubscription = "^-------!";
m.expect(source).toHaveSubscriptions(subs);

Of course it depends which version you are using and which marble library you use. (the syntax may be different). There is also a good blogpost which gives you a good overview of the whole topic. https://itnext.io/findings-about-rxjs-marble-testing-and-the-testscheduler-b23c6bdf6b49

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