title: c# Mocking Interface members of a concrete class with Moq
body: <p>I have an interface ITransaction as follows:</p>
  
  <pre><code>public interface ITransaction {
  
          DateTime EntryTime { get; }
  
          DateTime ExitTime { get; }
  }
  </code></pre>
  
  <p>and I have a derived class PaymentTransaction as follows:</p>
  
  <pre><code>public class PaymentTransaction : ITransaction {
  
          public virtual DateTime LastPaymentTime
          {
              get { return DateTime.Now; }
          }
  
          #region ITransaction Members
  
          public DateTime EntryTime
          {
              get  { throw new NotImplementedException(); }
          }
  
          public DateTime ExitTime
          {
              get  { throw new NotImplementedException(); }
          }
  
          #endregion
  }
  </code></pre>
  
  <p>I wanted to Mock all three properties of PaymentTransaction Object. </p>
  
  <p>I have tried the following, but it doesnt work:</p>
  
  <pre><code>var mockedPayTxn = new Mock&lt;PaymentTransaction&gt;();
  mockedPayTxn.SetUp(pt =&gt; pt.LastPaymentTime).Returns(DateTime.Now); // This works
  
  var mockedTxn = mockedPayTxn.As&lt;ITransaction&gt;();
  mockedTxn.SetUp(t =&gt; t.EntryTime).Returns(DateTime.Today); 
  mockedTxn.SetUp(t =&gt; t.ExitTime).Returns(DateTime.Today); 
  </code></pre>
  
  <p>but when I inject </p>
  
  <p>(mockedTxn.Object as PaymentTransaction)</p>
  
  <p>in the method I am testing (as it is only takes a PaymentTransaction and not ITransaction, I can't change it either) the debugger shows null reference for entry time and exit time. </p>
  
  <p>I was wondering if someone could help me please.</p>
  
  <p>Thanks in anticipation.</p>