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
因此不是重复建议
可能重复[如何进行单元测试的BeginInvoke上的动作](http://stackoverflow.com/questions/5885993/how-to-unit-test-begininvoke-on-an-action) – Orace
我我们将仔细研究它,但它正在讨论我相信的另一种BeginInvoke方法 – Zief