title: Mocking a Service using Moq in C#
body: <p>I have a service that is setup in this way.</p>
  
  <pre><code>public Interface IDataService : IDisposable
  {
      IQueryable&lt;T&gt; Set&lt;T&gt;() where T : class;
  
      IDbSet&lt;T&gt; WritableSet&lt;T&gt;() where T : class;
  }
  </code></pre>
  
  <p>IDataService is inherited by DataService.</p>
  
  <pre><code>public abstract class DataService : IDataService
  {
      public IDataContext DataContext { get; private set; }
  
      public IQueryable&lt;T&gt; Set&lt;T&gt;() where T : class
      {
          return DataContext.Set&lt;T&gt;().AsNoTracking();
      }
  
      public IDbSet&lt;T&gt; WritableSet&lt;T&gt;() where T : class
      {
          return DataContext.Set&lt;T&gt;();
      }
  
      public AddResult&lt;T&gt; Add&lt;T&gt;(T obj) where T : class, IPersistentEntity
      {
          if (obj == null)
              return new AddResult&lt;T&gt;() { IsValid = false };
          else
          {
              if (obj.Id == Guid.Empty)
                  WritableSet&lt;T&gt;().Add(obj);
  
              bool success = DataContext.SaveChanges() &gt; 0;
  
              return new AddResult&lt;T&gt;() { Entity = obj, IsValid = success };
          }
      }
  }
  </code></pre>
  
  <p>And The DataService is inherited by EntityService.</p>
  
  <pre><code>public class EntityService : DataService
  {
      public EntityService(IDataContext DataContext) : base(DataContext)
      {
      }
  
      public void EntityStarted(Guid Id)
      {
          var a = GetWriteableById&lt;Entity&gt;(Id);
          a.Status = 1;
          DataContext.SaveChanges();
      }
  }
  </code></pre>
  
  <p>This EntityService is used in one of my components. EntityService's object is created and passed to the component's constructor.</p>
  
  <p>I'm using Moq to perform some tests on the component and for that, the plan was to mock the EntityService such that the EntityService uses a fake db container with dummy data for database like operations. But, I'm not having the best idea to mock this with minimum amount of new code.</p>
  
  <p>The least appealing idea that I have is to create a fake EntityService class using the interface and have it's own implementation suitable for tests.</p>
  
  <p>Help is appreciated! :)</p>