title: How to unit test ViewModel consuming methods?
body: <p>I want to test a helper method that takes a ViewModel as an argument. The problem I'm having is that the test seems to require I instantiate and assign all of the Models that my ViewModel uses. In the example I give below, it's not such a big deal because there are only a few but in the actual VM I'm working on there are a ton. Is there some other way to do this so I don't have to create and assign every object?</p>
  
  <p>Example code for illustration purposes...</p>
  
  <p><strong>The Models</strong></p>
  
  <pre><code>public class Meal
  {
      public int MealID { get; set; }
  
      public string Title { get; set; }
      public decimal Cost { get; set; }
  }
  
  public class Beverage
  {
      public int BeverageID { get; set; }
  
      public string Title { get; set; }
      public decimal Cost { get; set; }
  }
  
  public class Desert
  {
      public int DesertID { get; set; }
  
      public string Title { get; set; }
      public decimal Cost { get; set; }
  }
  </code></pre>
  
  <p><strong>The ViewModel</strong></p>
  
  <pre><code>public class DinnerViewModel
  {
      public Meal Meal { get; set; }
      public Beverage Beverage { get; set; }
      public Desert Desert { get; set; }
  
      public decimal SalesTax { get; set; }
      public bool SeniorDiscount { get; set; }
  }
  </code></pre>
  
  <p><strong>The Helper</strong></p>
  
  <pre><code>public class Calculator
  {
      public decimal Total(DinnerViewModel dvm)
      {
          decimal subtotal = dvm.Meal.Cost + dvm.Beverage.Cost + dvm.Desert.Cost;
  
          if (dvm.SeniorDiscount)
          {
              subtotal = subtotal - (subtotal * 0.1M);
          }
  
          return subtotal + (subtotal * dvm.SalesTax);
      }
  }
  </code></pre>
  
  <p><strong>The Test</strong></p>
  
  <pre><code>[TestMethod]
  public void CalculatorReturnsCorrectTotalForNonSenior()
  {
      DinnerViewModel dvm = new DinnerViewModel();
      dvm.Meal.Cost = 7M;
      dvm.Beverage.Cost = 1M;
      dvm.Desert.Cost = 2M;
      dvm.SalesTax = 0.08M;
      dvm.SeniorDiscount = false;
  
      Calculator calc = new Calculator();
  
      decimal expected = 10.80M;
      decimal actual = calc.Total(dvm);
  
      Assert.AreEqual(expected, actual, "The actual value does not match the expected value.");
  }
  </code></pre>
  
  <p>This results in a "NullReferenceException" error. As I said, I can create and assign the necessary objects...</p>
  
  <pre><code>[...]
  Meal meal = new Meal();
  dvm.Meal = meal;
  dvm.Meal.Cost = 7M;
  [...]
  </code></pre>
  
  <p>...and, once they're all done, the test succeeds—but that seems like a ton of work on a larger VM and I get a feeling that there's gotta be something I can do to make this easier.</p>