0

I have the following event handler - which uses Dispatcher.BeginInvoke which I believe is different to another stack overflow question similar on BeginInvoke:

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?

EDIT: I didn't mention that this class inherits Dispatcher object, and therefore the BeginInvoke is not related to the answer given:

public class ImageViewModel : DispatcherObject, INotifyPropertyChanged

Hence not being a duplicate suggested

Zief
  • 172
  • 1
  • 9
  • 1
    possible duplicate of [How to Unit Test BeginInvoke on an Action](http://stackoverflow.com/questions/5885993/how-to-unit-test-begininvoke-on-an-action) – Orace Apr 14 '15 at 13:11
  • I'll have a closer look at it, but it is talking about a different BeginInvoke method I believe – Zief Apr 14 '15 at 13:29

1 Answers1

0

In the end I figured this answer is actually related to this answer Using the WPF Dispatcher in unit tests

Using a dispatcher helper util described in this answer, you can force it to process the queue and use this in the unit test.

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;
}
}
Community
  • 1
  • 1
Zief
  • 172
  • 1
  • 9