【问题标题】:Unit testing something with ObserveOnDispatcher使用 ObserveOnDispatcher 进行单元测试
【发布时间】:2012-03-07 16:50:30
【问题描述】:

我的视图模型中有一些代码如下:

miService.GetSomething(par1, par2)
.ObserveOnDispatcher()
.Subscribe(dt =>
 {
    DoSomething(dt);
 });

然后在我的测试中,我“嘲笑”我的服务如下:

miService.Setup(ms => ms.GetSomething(....))
.Returns(Observable.Return(XYZ));

问题是由于 ObserveOnDispatcher,订阅委托永远不会被执行。

我看过一些DispatcherFrame和PushFrame的代码,但问题是我不知道“在哪里”,我可以调用

frame.Continue = false;

【问题讨论】:

    标签: c# wpf unit-testing system.reactive dispatcher


    【解决方案1】:

    你可以试试

    var frame = new DispatcherFrame();
    Dispatcher.CurrentDispatcher.BeginInvoke(
      DispatcherPriority.Background, 
      new Action(() => frame.Continue = false));
    Dispatcher.PushFrame(frame);
    

    【讨论】:

    • 抱歉,这段代码甚至无法编译。如果我更改为: Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(o => { frame.Continue = false; return null; // 或 return null; }));我得到以下异常: System.Reflection.TargetParameterCountException : Parameter count mismatch.
    • 抱歉 - 应该先尝试编译我的代码。尝试更新版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2011-11-16
    • 2015-05-28
    • 2012-05-23
    • 2010-11-30
    • 2010-12-31
    相关资源
    最近更新 更多