问题描述
我正在尝试使用 TestScheduler
为我的 RxJs 函数编写单元测试.有很多相关的帖子,我正在关注这个帖子:
我不明白为什么我们需要将回调函数传递给 TestScheduler
构造函数.在帖子中,它提到它是 assertDeppEqual 函数,它告诉 TestScheduler 如何比较值.用于比较值的方法取决于您的测试框架.
但是这个解释我不清楚.不明白这个函数和我们实际写的测试逻辑有什么关系,比如下面这个案例:
在上述情况下,它断言 expectObservable().toBe()
.他们之间是什么关系?
让我们试试看什么expectObservable(observable, subscriptionMarbles)
和 run(callback)
方法可以.
expectObservable()
订阅 传递的observable
.传递给 subscribe()
方法的回调方法填充 actual
数组(这是你在传递给 TestScheduler
构造函数的回调中得到的).>
actual
数组 获取 到 this.flushTests
数组(包裹在 flushTest
对象中).flushTests
是一组必须执行的测试(请记住,您可以多次调用 expectObservable()
或 expectSubscriptions()
方法).如果您正在使用 run()
方法(并且您基于图像这样做),则在执行 run()
回调后,this.flush叫.此方法通过运行您的测试noreferrer">调用
this.assertDeepEqual()
您提供给 TestScheduler
构造函数的回调.
传入来自 test
的 actual
和 expected
值. expected
以相同的方法填充其中 actual
被填充,就在它之后.
因此,调用 expectObservable()
只会准备 Observable 值并将其转换为可比较的值(actual
和 expected
值的数组).正如官方文档所说,您有责任根据您使用的测试框架进行比较.
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?
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.
这篇关于RxJS 单元测试:传递给 TestScheduler 的回调的功能是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!