The right way to use WPF Manager in unit tests

I am trying to follow the recommendations Using WPF Manager in Unit Tests to run my nUnit test.

When I write my unit test, as shown below, it works:

[Test]
public void Data_Should_Contain_Items()
{
    DispatcherFrame frame = new DispatcherFrame();
        PropertyChangedEventHandler waitForModelHandler = delegate(object sender, PropertyChangedEventArgs e)
        {
          if (e.PropertyName == "Data")
          {
            frame.Continue = false;
          }
        };
    _myViewModel.PropertyChanged += waitForModelHandler;
    Dispatcher.PushFrame(frame);

    Assert.IsTrue(_myViewModel.Data.Count > 0, "Data item counts do not match");
}

However, if I try to use the DispatcherUtil clause, this will not work:

[Test]
public void Data_Should_Contain_Items()
{
    DispatcherUtil.DoEvents();
    Assert.IsTrue(_myViewModel.Data.Count > 0, "Data item counts do not match");
}

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

When I use DispatcherUtil, it looks like the ExitFrame call is too early before the data is ready.

Am I not using DispatcherUtil correctly? This seems like the best method to handle the dispatcher and then wait for callbacks from the view model.

+7
source share
2 answers

, . , , :

Dispatcher.CurrentDispatcher.BeginInvoke(..

- - , .

( , Unity ..). , . , . , Action.BeginInvoke. IAsyncResults, BeginInvoke.
, , , .

, . , .

+7

. , Frames Async Dispatcher, DispatcherUtil. DoEventsSync() -method , , , :

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

        public static void DoEventsSync()
        {
            var frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background,
                new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
        }

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

DispatcherUtil.DoEventsSync(); DispatcherUtil.DoEvents(); -. , , -. .

, DispatcherUtil.DoEvents(); DispatcherUtil.DoEventsAsync(); BeginInvoke(..) -method

+2

All Articles