您会为 Rx.Observable 创建服务吗?

发布于 2024-12-06 23:29:31 字数 437 浏览 0 评论 0原文

我在 VM 中有以下代码..

Observable
                    .Timer(remainingTimeSpanForNextHour, TimeSpan.FromHours(1))
                    .Timestamp()
                    .Subscribe( x => FillBookingData());

我的问题是 ~

  1. 您认为我们需要测试此代码吗?如果我们计划测试这段代码,是不是就像我们正在尝试测试 Rx 一样?或许。我们应该只测试这个值“remainingTimeSpanForNextHour”

  2. 假设我们应该测试这段代码。您认为创建像 IObservableService 这样的服务是个好主意吗?

I have the following code in VM..

Observable
                    .Timer(remainingTimeSpanForNextHour, TimeSpan.FromHours(1))
                    .Timestamp()
                    .Subscribe( x => FillBookingData());

My questions are ~

  1. 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"

  2. Let's say we should test this code. Do you think it's good idea to create the service like IObservableService ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2024-12-13 23:29:32

测试您的代码总是一个好主意,但是对于可观察量,如果您的代码全部位于一个方法或代码块中,则可能会有点困难。

您应该尝试分离出此查询的组件 - 对我来说,有三个组件:

  1. 参数
  2. Observable
  3. Observer

因此,编写测试以确保您获得正确的参数值。

然后编写测试以确保您的查询根据相似的输入值集。我不会编写一个必须等​​待一个小时才能到达值的测试,因此将小时更改为秒等。

然后编写测试以确保观察者正常工作。

现在,就编写 IObservableService 接口/实现而言,我认为这不是一件好事。相反,我会专注于编写一项或多项服务,以抽象出您在功能基础上尝试做的事情,以便您保持 DRY(不要重复自己)。

因此,我认为 ITimerService 可能会有用。

public interface ITimerService
{
    IDisposable Subscribe(TimeSpan dueTime, TimeSpan period, Action action);
}

显然,它的设计是为了适应 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:

  1. Parameters
  2. Observable
  3. Observer

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.

public interface ITimerService
{
    IDisposable Subscribe(TimeSpan dueTime, TimeSpan period, Action action);
}

Clearly it is designed to fit in with Rx - the signature is similar to Observable.Timer crossed with IObservable.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.

绿萝 2024-12-13 23:29:32

使用 虚拟时间 - 您可以忽略 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文