title: Unit testing a method with Moq
body: <p>I'm trying to learn how to do Unit testing with C# and Moq, and I've built a little test situation. Given this code:</p>
  
  <pre><code>public interface IUser
  {
  
      int CalculateAge();
      DateTime DateOfBirth { get; set; }
      string Name { get; set; }
  }
  
  public class User : IUser
  {
      public DateTime DateOfBirth { get; set; }
      string Name { get; set; }
  
      public int CalculateAge()
      {
          return DateTime.Now.Year - DateOfBirth.Year;
      }
  }
  </code></pre>
  
  <p>I want to test the method <code>CalculateAge()</code>. To do this, I thought I should try giving a default value to the <code>DateOfBirth</code> property by doing this in my test method:</p>
  
  <pre><code>var userMock = new Mock&lt;IUser&gt;();
  userMock.SetupProperty(u =&gt; u.DateOfBirth, new DateTime(1990, 3, 25)); //Is this supposed to give a default value for the property DateOfBirth ?
  Assert.AreEqual(22, userMock.Object.CalculateAge());
  </code></pre>
  
  <p>But when It comes to the assertion, the value of <code>CalculateAge()</code> equals 0, although <code>DateOfBirth</code> equals <code>new DateTime(1990, 3, 25)</code>.</p>
  
  <p>I know this may look like a silly example, but whatever... I thought I could use mocking to give values to not-yet-developed method/properties in my objects, so the testing of a method wouldn't depend on another component of my class, or even setting up a default context for my object (hence the name of the user here...) Am I approaching this problem the wrong way?</p>
  
  <p>Thanks.</p>