Using the WPF Dispatcher in unit tests

前端 未结 16 2053
北恋
北恋 2020-11-27 02:48

I\'m having trouble getting the Dispatcher to run a delegate I\'m passing to it when unit testing. Everything works fine when I\'m running the program, but, during a unit te

相关标签:
16条回答
  • 2020-11-27 03:14

    It's a bit old post, BeginInvoke is not a preferable option today. I was looking for a solution for mocking and had't found anything for InvokeAsync:

    await App.Current.Dispatcher.InvokeAsync(() => something );

    I've added new Class called Dispatcher, implementing IDispatcher, then inject into viewModel constructor:

    public class Dispatcher : IDispatcher
    {
        public async Task DispatchAsync(Action action)
        {
            await App.Current.Dispatcher.InvokeAsync(action);
        }
    }
    public interface IDispatcher
        {
            Task DispatchAsync(Action action);
        }
    

    Then in test I've injected MockDispatcher into viewModel in constructor:

    internal class MockDispatcher : IDispatcher
        {
            public async Task DispatchAsync(Action action)
            {
                await Task.Run(action);
            }
        }
    

    Use in the view model:

    await m_dispatcher.DispatchAsync(() => something);
    
    0 讨论(0)
  • 2020-11-27 03:15

    Winforms has a very easy and WPF compatible solution.

    From your unit test project, reference System.Windows.Forms.

    From your unit test when you want to wait for dispatcher events to finish processing, call

            System.Windows.Forms.Application.DoEvents();
    

    If you have a background thread that keeps adding Invokes to the dispatcher queue, then you'll need to have some sort of test and keep calling DoEvents until the background some other testable condition is met

            while (vm.IsBusy)
            {
                System.Windows.Forms.Application.DoEvents();
            }
    
    0 讨论(0)
  • 2020-11-27 03:16

    Simplest way I found is to just add a property like this to any ViewModel that needs to use the Dispatcher:

    public static Dispatcher Dispatcher => Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher;
    

    That way it works both in the application and when running unit tests.

    I only had to use it in a few places in my entire application so I didn't mind repeating myself a bit.

    0 讨论(0)
  • 2020-11-27 03:17

    We've solved this issue by simply mocking out the dispatcher behind an interface, and pulling in the interface from our IOC container. Here's the interface:

    public interface IDispatcher
    {
        void Dispatch( Delegate method, params object[] args );
    }
    

    Here's the concrete implementation registered in the IOC container for the real app

    [Export(typeof(IDispatcher))]
    public class ApplicationDispatcher : IDispatcher
    {
        public void Dispatch( Delegate method, params object[] args )
        { UnderlyingDispatcher.BeginInvoke(method, args); }
    
        // -----
    
        Dispatcher UnderlyingDispatcher
        {
            get
            {
                if( App.Current == null )
                    throw new InvalidOperationException("You must call this method from within a running WPF application!");
    
                if( App.Current.Dispatcher == null )
                    throw new InvalidOperationException("You must call this method from within a running WPF application with an active dispatcher!");
    
                return App.Current.Dispatcher;
            }
        }
    }
    

    And here's a mock one that we supply to the code during unit tests:

    public class MockDispatcher : IDispatcher
    {
        public void Dispatch(Delegate method, params object[] args)
        { method.DynamicInvoke(args); }
    }
    

    We also have a variant of the MockDispatcher which executes delegates in a background thread, but it's not neccessary most of the time

    0 讨论(0)
  • 2020-11-27 03:19

    Creating a DipatcherFrame worked great for me:

    [TestMethod]
    public void Search_for_item_returns_one_result()
    {
        var searchService = CreateSearchServiceWithExpectedResults("test", 1);
        var eventAggregator = new SimpleEventAggregator();
        var searchViewModel = new SearchViewModel(searchService, 10, eventAggregator) { SearchText = searchText };
    
        var signal = new AutoResetEvent(false);
        var frame = new DispatcherFrame();
    
        // set the event to signal the frame
        eventAggregator.Subscribe(new ProgressCompleteEvent(), () =>
           {
               signal.Set();
               frame.Continue = false;
           });
    
        searchViewModel.Search(); // dispatcher call happening here
    
        Dispatcher.PushFrame(frame);
        signal.WaitOne();
    
        Assert.AreEqual(1, searchViewModel.TotalFound);
    }
    
    0 讨论(0)
  • 2020-11-27 03:19

    I suggest adding one more method to the DispatcherUtil call it DoEventsSync() and just call the Dispatcher to Invoke instead of BeginInvoke. This is needed if you really have to wait until the Dispatcher processed all frames. I am posting this as another Answer not just a comment, since the whole class is to long:

        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;
            }
        }
    
    0 讨论(0)
提交回复
热议问题