您会为 Rx.Observable 创建服务吗?
我在 VM 中有以下代码..
Observable
.Timer(remainingTimeSpanForNextHour, TimeSpan.FromHours(1))
.Timestamp()
.Subscribe( x => FillBookingData());
我的问题是 ~
您认为我们需要测试此代码吗?如果我们计划测试这段代码,是不是就像我们正在尝试测试 Rx 一样?或许。我们应该只测试这个值“remainingTimeSpanForNextHour”
假设我们应该测试这段代码。您认为创建像 IObservableService 这样的服务是个好主意吗?
I have the following code in VM..
Observable
.Timer(remainingTimeSpanForNextHour, TimeSpan.FromHours(1))
.Timestamp()
.Subscribe( x => FillBookingData());
My questions are ~
Do you think we need to test this code? Would it be like we are trying to test Rx if we are planning to test this code? maybe. we should just test only this value "remainingTimeSpanForNextHour"
Let's say we should test this code. Do you think it's good idea to create the service like IObservableService ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试您的代码总是一个好主意,但是对于可观察量,如果您的代码全部位于一个方法或代码块中,则可能会有点困难。
您应该尝试分离出此查询的组件 - 对我来说,有三个组件:
因此,编写测试以确保您获得正确的参数值。
然后编写测试以确保您的查询根据相似的输入值集。我不会编写一个必须等待一个小时才能到达值的测试,因此将小时更改为秒等。
然后编写测试以确保观察者正常工作。
现在,就编写 IObservableService 接口/实现而言,我认为这不是一件好事。相反,我会专注于编写一项或多项服务,以抽象出您在功能基础上尝试做的事情,以便您保持 DRY(不要重复自己)。
因此,我认为
ITimerService
可能会有用。显然,它的设计是为了适应 Rx - 签名类似于与
IObservable.Subscribe
交叉的Observable.Timer
。它将使用您现有的查询,只是使用输入参数。您应该发现测试此代码非常容易。
让我知道这对您来说是否是一个好的起点。
It's always a good idea to test your code, but with observables it can be a little hard if your code is all in the one method or code block.
You should try to separate out the components of this query - and for me there are three components:
So write tests to ensure you get the right parameter values coming in.
Then write tests that ensure that your query produces values according to a similar set of input values. I wouldn't write a test that has to wait an hour for a value to arrive, so change hours to seconds, etc.
Then write tests that make sure that the observer works.
Now, as far as writing an
IObservableService
interface/implementation I think that is not a good thing. I'd instead focus on writing one or more service that abstract away what you're trying to do on a functional basis so that you are being DRY (Don't Repeat Yourself).So, I would think an
ITimerService
might be useful.Clearly it is designed to fit in with Rx - the signature is similar to
Observable.Timer
crossed withIObservable.Subscribe
. It would use your existing query, just made to use the input parameters.You should find testing this code to be quite easy.
Let me know if this is a good starting point for you.
使用 虚拟时间 - 您可以忽略 ReactiveUI 位,使用 AdvanceTo 并找到一种方法将您正在使用的 IScheduler 注入到代码中(Timer() 和必须为您的 TestScheduler 对象提供 Timestamp())
尽管这看起来很痛苦,但最终结果是您可以测试通常需要时间才能执行的代码,立即,并且您的测试将具有相同的结果每次都有结果。
Testing a timer is easy using Virtual Time - you can ignore the ReactiveUI bits, use AdvanceTo and figure out a way to inject the IScheduler you're using into your code (both Timer() and Timestamp() must be given your TestScheduler objects)
Even though this seems like a pain, the end result is that you can test code that will normally take time to execute, instantly and your tests will have the same result every time.