本文介绍了使用ObserveOnDispatcher进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的视图模型中有一些代码,如下所示:
I've some code in my view model as follows:
miService.GetSomething(par1, par2)
.ObserveOnDispatcher()
.Subscribe(dt =>
{
DoSomething(dt);
});
然后在我的测试中,我正在模拟我的服务,如下所示:
Then in my test, I'm "mocking" my service as follows:
miService.Setup(ms => ms.GetSomething(....))
.Returns(Observable.Return(XYZ));
问题是由于ObserveOnDispatcher,订阅委托从未执行。
The problem is that due to the ObserveOnDispatcher, the subscribe delegate is never executed.
我已经用DispatcherFrame和PushFrame看到了一些代码,但是问题是我不知道哪里,我可以打电话
I've seen some code with DispatcherFrame and PushFrame, but the problem is that I don't know "where", I can call
frame.Continue = false;
推荐答案
您可以尝试
var frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action(() => frame.Continue = false));
Dispatcher.PushFrame(frame);
这篇关于使用ObserveOnDispatcher进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!