mardi 14 avril 2015

Asynchronous method testing in C#

I have the following event handler:



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


and then:



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


And to test this, the following test method:



[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 );
}


Clearly this will not work, as the source_load_complete_ui method is invoked asyncronously. However I can't figure out after searching around how best to test this and wait for the asynchronous method to be called?


Aucun commentaire:

Enregistrer un commentaire