问题
I'm trying to use TestScheduler
to write unit test for my RxJs functions. There are many posts related to it, and I'm following this post : https://medium.com/@kevinkreuzer/marble-testing-with-rxjs-testing-utils-3ae36ac3346a
There is one confusing point for the following part:
I don't understand why we need to pass the callback function into TestScheduler
constructor. In the post, it mentioned that it is assertDeppEqual function which tells the TestScheduler how to compare values. The methods used to compare values depends on your testing framework.
But this explanation is not clear for me. I don't figure out what's the relationship between this function and the actual test logic we write, for example the following case:
in the above case, it assert that expectObservable().toBe()
. what's the relationship between them?
回答1:
Let's try to see what expectObservable(observable, subscriptionMarbles) and run(callback) methods do.
expectObservable()
subscribes to a passed observable
. The callback methods passed to subscribe()
method populate actual
array (this is what you get in a callback passed to TestScheduler
constructor).
actual
array gets pushed to this.flushTests
array (wrapped in flushTest
object). flushTests
is an array of tests that have to be executed (remember, you can call multiple times expectObservable()
or expectSubscriptions()
methods). If you're using run()
method (and you do based on the image), after a run()
callback is executed, this.flush() method is called. This method runs your tests by calling this.assertDeepEqual()
callback that you provided to TestScheduler
constructor.
The actual
and expected
values from the test
are passed in. expected
is populated in the same method where actual
is populated, just after it.
So, calling expectObservable()
only prepares and converts Observable values to something comparable (an array of actual
and expected
values). It is your responsibility, as the official documentation says, to do the comparison based on the testing framework you're using.
来源:https://stackoverflow.com/questions/58306240/rxjs-unit-testing-whats-the-functionality-of-the-callback-passed-to-testschedu