title: Moq, abstract class and virtual properties
body: <p>I am using <code>Moq</code> framework for Mocking.</p>
  
  <p>I have a <code>SocialWorker</code> which derives from an <code>abstract DataWorker</code> class.</p>
  
  <p>The <code>SocialWorker</code> has reference to couple of <code>Repositories</code> as a <code>virtual property</code></p>
  
  <p>I am trying to run the following test:</p>
  
  <pre><code>private Place _place;
  private Mock&lt;IRepository&lt;Resources.Data.Place&gt;&gt; _placeRepositoryMock;
  private Mock&lt;SocialWorker&gt; _socialWorkerMock;
  
  [SetUp]
  public void SetUp()
  {
      _place = new Place {Name = "A"};
  
      _socialWorkerMock = new Mock&lt;SocialWorker&gt; {DefaultValue = DefaultValue.Mock};
  
      IRepository&lt;Resources.Data.Place&gt; placeRepository = _socialWorkerMock.Object.PlaceRepository;
  
      _placeRepositoryMock = Mock.Get(placeRepository);
  
      _placeRepositoryMock.Setup(
          repository =&gt;
          repository.Find(It.IsAny&lt;Expression&lt;Func&lt;Resources.Data.Place, bool&gt;&gt;&gt;(), null, null))
          .Returns(new[] {new Resources.Data.Place()});
  }
  
  [Test]
  public void AddShouldAddANewPlace()
  {
      var placeManager = new PlaceManager(_socialWorkerMock.Object);
  
      object placeEntity = placeManager.Add(_place);
  
      placeEntity.GetType().Should().Equal(typeof (Resources.Data.Place));
  
      _socialWorkerMock.Verify(
          socialWorker =&gt; socialWorker.PlaceRepository.Add(It.IsAny&lt;Resources.Data.Place&gt;()), Times.Once());
  
      _placeRepositoryMock.Verify(
          placeRepository =&gt;
          placeRepository.Find(p =&gt; p.Name.Equals(_place.Name), null, null).First(),
          Times.Once());
  }
  </code></pre>
  
  <p>The last verification of this test fails with this error:</p>
  
  <pre><code>System.NotSupportedException : Invalid verify on a non-virtual (overridable in VB) member: placeRepository =&gt; placeRepository.Find(p =&gt; p.Name.Equals(._place.Name), null, null).First&lt;Place&gt;()
  </code></pre>
  
  <p>I am fairly new to Moq and unit testing in general.</p>
  
  <p>Here is the relevant code for your reference:</p>
  
  <p><code>IUnitOfWork</code></p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      void CommitChanges();
  }
  </code></pre>
  
  <p><code>IRepository</code></p>
  
  <pre><code>public interface IRepository&lt;T&gt;
  {
      void Add(T entity);
  
      void Delete(T entity);
  
      IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; filter = null,
                          Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderBy = null,
                          IList&lt;string&gt; includedProperties = null);
  
      T FindById(object id);
  
      void Update(T entity);
  }
  </code></pre>
  
  <p><code>DataWorker</code></p>
  
  <pre><code>public abstract class DataWorker : IUnitOfWork
  {
      protected ObjectContext ObjectContext;
      private bool _disposed;
  
      protected DataWorker()
      {
          ObjectContext = new ObjectContext(ConfigurationManager.ConnectionStrings["DataEntities"].ConnectionString);
      }
  
      ~DataWorker()
      {
          . . .
      }
  
      protected virtual void Dispose(bool disposing)
      {
          . . .
      }
  
      public virtual void CommitChanges()
      {
          ObjectContext.SaveChanges();
      }
  
      public void Dispose()
      {
          . . .
      }
  }
  </code></pre>
  
  <p><code>DataRepository</code></p>
  
  <pre><code>public class DataRepository&lt;T&gt; : IRepository&lt;T&gt; where T : EntityObject
  {
      private readonly ObjectSet&lt;T&gt; _objectSet;
  
      public DataRepository(ObjectContext objectContext)
      {
              _objectSet = objectContext.CreateObjectSet&lt;T&gt;();
      }
  
      public void Add(T entity)
      {
          . . .
      }
  
      public void Delete(T entity)
      {
          . . .
      }
  
      public virtual IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; filter = null,
                                 Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderBy = null,
                                 IList&lt;string&gt; includedProperties = null)
      {
          . . .
      }
  
      public T FindById(object id)
      {
          . . .
      }
  
  
      public void Update(T entity)
      {
          . . .
      }
  }
  </code></pre>
  
  <p><code>SocialWorker</code></p>
  
  <pre><code>public class SocialWorker : DataWorker
  {
      private IRepository&lt;ContactRequest&gt; _contactRequestRepository;
      private IRepository&lt;Place&gt; _placeRepository;
      private IRepository&lt;User&gt; _userRepository;
  
      public virtual IRepository&lt;ContactRequest&gt; ContactRequestRepository
      {
          get
          {
              return _contactRequestRepository ??
                     (_contactRequestRepository = new DataRepository&lt;ContactRequest&gt;(ObjectContext));
          }
      }
  
      public virtual IRepository&lt;Place&gt; PlaceRepository
      {
          get { return _placeRepository ?? (_placeRepository = new DataRepository&lt;Place&gt;(ObjectContext)); }
      }
  
      public virtual IRepository&lt;User&gt; UserRepository
      {
          get { return _userRepository ?? (_userRepository = new DataRepository&lt;User&gt;(ObjectContext)); }
      }
  }
  </code></pre>