对 Observable (Subject) 进行单元测试
我有一个 Repository
,其中有一个名为 Mutations 的公共 Subject
,可以订阅它。将可观察对象视为 Item
流更容易,其中源主要是套接字连接。
我使用诸如 Repository.Mutations.Subscribe(item => DoWhatever(item));
之类的内容订阅流。
我有一个模拟数据源,我想将 Items 推送到该数据源以测试存储库中的 Stream/Observable 是否正在执行其工作。
我已经阅读了很多关于在 Rx 中使用 TestScheduler()
的内容,但是当我尝试创建单元测试以及这个调度程序如何使用我自己的代码发挥作用时,我完全迷失了。
诚然,我是 Rx 的初学者,但我希望有人能给我一两个提示。
I got a Repository
with a public Subject<Item>
called Mutations to which can be subscribed. It's easier to see the observable as a stream of Item
of which the source will primary be a socket connection.
I subscribe to the stream using something like Repository.Mutations.Subscribe(item => DoWhatever(item));
.
I got a mocked datasource to which I want to push Items to test whether this Stream/Observable in the repository is doing it's job.
I've been reading a lot about using the TestScheduler()
in Rx but I'm completely lost when trying to create a unit test and how this scheduler would come into play using my own code.
Admittedly I'm a beginner with Rx but I hope someone would be able to give me a hint or two.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要查看一个项目是否是由
Mutations
属性生成的,您可以像您所做的那样,但只需在测试代码中验证是否已调用实际的 subscribe 方法,即类似:If what you're after is to see if an item is produced by the
Mutations
property, you can do like you have, but just verify, in your test code, that the actual subscribe method has been called, i.e. something like: