Using the WPF Dispatcher in unit tests

前端 未结 16 2132
北恋
北恋 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: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();
            }
    

提交回复
热议问题