2015-04-14 32 views
0

我有以下的事件处理程序 - 它使用Dispatcher.BeginInvoke我相信这是对的BeginInvoke类似的另一个堆栈溢出问题不同:异步方法测试

private void source_load_complete() 
    { 
     Dispatcher.BeginInvoke(new NotificationDelegate(source_load_complete_ui), null); 
    } 

然后:

private void source_load_complete_ui() 
    { 
     m_image.Image = m_bmp.CreateBitmap(); 
     m_image.UpdateImage(null); 
     m_image.LoadComplete = true; 
     raise_property_changed("CurrentImage"); 
    } 

并测试这个,下面的测试方法:

[TestMethod] 
    public void ImageVM_SourceLoadTests() 
    { 
     ImageViewModel ivm = new ImageViewModel(); 
     List<string> eventList = new List<string>(); 

     ivm.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
     { 
      eventList.Add(e.PropertyName); 
     }; 

     // check the model is set up correctly so the event in the constructor works 
     CustomImage model = ivm.ImageModel as CustomImage; 
     WriteableBitmap bmp = model.Image; 

     if(model.ImageSource.LoadComplete != null) 
     { 
// fire off the event we want to test is handled correctly 
      model.ImageSource.LoadComplete(); 
     } 

     Assert.IsTrue(model.LoadComplete == true); 
     Assert.IsTrue(model.Image == bmp); 
     Assert.IsTrue(eventList.Count == 1); 
    } 

显然这不会窝窝rk,因为source_load_complete_ui方法是异步调用的。然而,我无法找出如何最好的测试,并等待异步方法被调用?

编辑: 我没有提到,这个类继承Dispatcher对象,因此BeginInvoke的是不相关的给出了答案:

public class ImageViewModel : DispatcherObject, INotifyPropertyChanged 

因此不是重复建议

+1

可能重复[如何进行单元测试的BeginInvoke上的动作](http://stackoverflow.com/questions/5885993/how-to-unit-test-begininvoke-on-an-action) – Orace

+0

我我们将仔细研究它,但它正在讨论我相信的另一种BeginInvoke方法 – Zief

回答

0

在end我认为这个答案实际上与这个答案相关Using the WPF Dispatcher in unit tests

使用本答案中描述的调度器帮助程序util,可以强制它处理队列并在单元测试中使用它。的

public static class DispatcherUtil 
{ 

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] 
public static void DoEvents() 
{ 
    DispatcherFrame frame = new DispatcherFrame(); 
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, 
     new DispatcherOperationCallback(ExitFrame), frame); 
    Dispatcher.PushFrame(frame); 
} 

private static object ExitFrame(object frame) 
{ 
    ((DispatcherFrame)frame).Continue = false; 
    return null; 
} 
}