mercredi 8 juillet 2015

How to Unit test ViewModel with async initialization in WPF

I have created a sample WPF MVVM project which I now want to Unit test. The viewmodels load the data asynchronously in the constructor:

public class CustomerOverviewViewModel
{
   public CustomerOverviewViewModel()
   {
       var t = LoadFullCustomerListAsync();
   }
   public async Task LoadFullCustomerListAsync()
   {
      List<BL_acc> customers = await Task.Run(() => // Query from db);
   }
}

In WPF, this works like a charm. When I want to create a unit test for this viewmodel, I create the object by calling its default constructor:

  [TestMethod]
  public void Test()
  {
      customerOverviewViewModel = new CustomerOverviewViewModel();
      // Do the test
  }

However, the unit test has no way of knowing when the async method is finished. Can this be fixed using constructor initialization or should I use a different pattern?

Aucun commentaire:

Enregistrer un commentaire