【发布时间】:2010-11-09 13:31:41
【问题描述】:
我无法让 Dispatcher 运行我在单元测试时传递给它的委托。当我运行程序时一切正常,但是,在单元测试期间,以下代码将不会运行:
this.Dispatcher.BeginInvoke(new ThreadStart(delegate
{
this.Users.Clear();
foreach (User user in e.Results)
{
this.Users.Add(user);
}
}), DispatcherPriority.Normal, null);
我的 viewmodel 基类中有这段代码来获取 Dispatcher:
if (Application.Current != null)
{
this.Dispatcher = Application.Current.Dispatcher;
}
else
{
this.Dispatcher = Dispatcher.CurrentDispatcher;
}
我需要做些什么来初始化 Dispatcher 以进行单元测试吗? Dispatcher 从不运行委托中的代码。
【问题讨论】:
-
我没有收到任何错误。只是在 Dispatcher 上传递给 BeginInvoke 的内容永远不会运行。
-
老实说,我还没有对使用调度程序的视图模型进行单元测试。调度程序是否可能没有运行。会在您的测试帮助中调用 Dispatcher.CurrentDispatcher.Run() 吗?我很好奇,所以如果你得到结果,请发布结果。
标签: c# .net wpf unit-testing dispatcher