2016-10-04 2 views
1

J'utilise MVVM Light 5.2 dans Visual Studio 2012. Mes tests unitaires sont des tests MS et je n'arrive pas à comprendre comment tester mes méthodes asynchrones, car DispatcherHelper n'invoque pas mon action. En utilisant le test suivant, Thread.Sleep n'est jamais atteint dans le débogage. Dans les sources de lumière MVVM, DispatcherHelper.CheckBeginInvokeOnUi appelle UIDispatcher.BeginInvoke (action), et rien ne se produit jamais. Qu'est-ce que je fais de mal?Unité testant une méthode asynchrone qui appelle DispatcherHelper.CheckBeginInvokeOnUI

[TestMethod] 
    public void TestMethod1() 
    { 
     DispatcherHelper.Initialize(); 
     TestedMethod(); 
     // Do assert here 
    } 

    void TestedMethod() 
    { 
     ThreadPool.QueueUserWorkItem((o) => 
     { 
      // Do stuff 
      DispatcherHelper.CheckBeginInvokeOnUI(() => 
      { 
       // Do stuff 
       Thread.Sleep(1); // Breakpoint here 
      }); 
     }); 
    } 

Répondre

1

Dans le cas où il peut aider, comme je ne voulais pas modifier MVVM sources de lumière, j'ai finalement écrit une procuration sur DispatcherHelper qui invoque directement l'action quand il est initialisé dans une méthode d'essai. Il gère également le mode de conception.

J'ai juste eu à chercher/remplacer chaque DispatcherHelper par UIDispatcher, et c'est tout.

Voici le code:

public static class UIDispatcher 
{ 
    private static bool _isTestInstance; 

    /// <summary> 
    ///  Executes an action on the UI thread. If this method is called from the UI 
    ///  thread, the action is executed immendiately. If the method is called from 
    ///  another thread, the action will be enqueued on the UI thread's dispatcher 
    ///  and executed asynchronously. 
    ///  For additional operations on the UI thread, you can get a reference to the 
    ///  UI thread's dispatcher thanks to the property GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher 
    /// </summary> 
    /// <param name="action">The action that will be executed on the UI thread.</param> 
    public static void CheckBeginInvokeOnUI(Action action) 
    { 
     if (action == null) 
     { 
      return; 
     } 
     if ((_isTestInstance) || (ViewModelBase.IsInDesignModeStatic)) 
     { 
      action(); 
     } 
     else 
     { 
      DispatcherHelper.CheckBeginInvokeOnUI(action); 
     } 
    } 

    /// <summary> 
    ///  This method should be called once on the UI thread to ensure that the GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher 
    ///  property is initialized. 
    ///  In a Silverlight application, call this method in the Application_Startup 
    ///  event handler, after the MainPage is constructed. 
    ///  In WPF, call this method on the static App() constructor. 
    /// </summary> 
    /// <param name="isTestInstance"></param> 
    public static void Initialize(bool isTestInstance = false) 
    { 
     _isTestInstance = isTestInstance; 
     if (!_isTestInstance) 
      DispatcherHelper.Initialize(); 
    } 

    /// <summary> 
    /// Resets the class by deleting the GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher 
    /// </summary> 
    public static void Reset() 
    { 
     if (!_isTestInstance) 
      DispatcherHelper.Reset(); 
    } 
}