Posts

Showing posts from April, 2013

C# How to unit test Dispatcher

Dispatcher provides services for managing the queue of work items for a thread. Sometimes to execute a task on a background thread.  Any task executed by calling Dispatcher.BeginInvoke(...) in unit test does not get executed until you do some wire-up. This is how you would unit test code with Dispatcher. using  System; using  System.Windows.Threading; using  Microsoft.VisualStudio.TestTools.UnitTesting; namespace  UnitTesting.Tests {     [ TestClass ]      public   class   DispatcherUtilTest     {          private   ClassThatUsesDispatcher  _subject;         [ TestInitialize ]          public   void  TestInitialize()         {             _subject =  new   ClassThatUsesDispatcher ();         }         [ TestMethod ]          public   void  UseDispatcher()         {              // Arrange              var  backgroundWorkDone =  false ;             _subject.BackgroungWorkAction = () => { backgroundWorkDone =  true ; };              // Act             _subject.DoWork(