我在单元测试时遇到了一个问题,无法使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未能执行委托中的代码。