title: How can I test an MvvmCross ViewModel that uses its ViewDispatcher property?
body: <p>I have an MvvmCross ViewModel that inherits from <code>MvxBase</code> and use's its <code>ViewDispatcher</code> property like this:</p>
  
  <pre><code>    /// Handles a timer elapsed event
      private void TimerOnElapsed(object sender, EventArgs eventArgs)
      {
        ViewDispatcher.RequestMainThreadAction(UpdateEllapsedTime);
      }
  </code></pre>
  
  <p>The problem is that ViewDispatcher is null when I try to use this ViewModel in a unit test.  I am creating and registering a mock dispatcher in the test, but the ViewModel's <code>ViewDispatcher</code> property is by a parent class in the framework to be <code>MvxMainThreadDispatcher.Instance</code> rather then being resolved from the IoC container.</p>
  
  <p>The workaround I am currently using is the following:</p>
  
  <pre><code>    private void TimerOnElapsed(object sender, EventArgs eventArgs)
      {
          if (ViewDispatcher != null)
          {
              ViewDispatcher.RequestMainThreadAction(UpdateEllapsedTime);
          }
          else
          {
              UpdateEllapsedTime();
          }
      }
  </code></pre>
  
  <p>I don't want to add code into a class for the sole purpose of getting unit tests to work, but I don't think its a big deal in this case.  However, I'm still wondering if there is a better way to make this ViewModel testable.</p>