title: My Mocked DbContext writes to db in Unit Test using Moq
body: <p>I am new to mocking and using Moq. This is my first time trying to Mock the add functionality. I have the mocks setup and it works for read functions, but when I try to unit test an add method, it saves the changes to the db.</p>
  
  <p>How do I mock the add method? </p>
  
  <p>I think I have to Mock my <code>UnitOfWork</code> class which has the <code>SaveChanges()</code> method. I want to know how I have to setup the mock to intercept the call to <code>SaveChanges()</code> and not save to db.</p>
  
  <p>Here are the Mocks I have so far</p>
  
  <pre><code>[SetUp]
      public void SetUp()
      {
          addCount = 0;
  
          IEnumerable&lt;Platform&gt; platformList = new List&lt;Platform&gt;(){
              new Platform() { Id = 1, Name = "Unknown"},
              new Platform() { Id =2, Name = "Amazon"},
              new Platform() { Id = 3, Name = "Prime Pantry"}
          };
          var platformData = platformList.AsQueryable();
  
          var mockPlatformSet = new Mock&lt;DbSet&lt;Platform&gt;&gt;();
          mockPlatformSet.As&lt;IQueryable&lt;Platform&gt;&gt;().Setup(m =&gt; m.Provider).Returns(platformData.Provider);
          mockPlatformSet.As&lt;IQueryable&lt;Platform&gt;&gt;().Setup(m =&gt; m.Expression).Returns(platformData.Expression);
          mockPlatformSet.As&lt;IQueryable&lt;Platform&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(platformData.ElementType);
          mockPlatformSet.As&lt;IQueryable&lt;Platform&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(platformData.GetEnumerator());
          mockPlatformSet.Setup(m =&gt; m.Add(It.IsAny&lt;Platform&gt;())).Callback(() =&gt; addCount++);
  
          var mockContext = new Mock&lt;ApplicationDbContext&gt;(){ CallBase = true };
          mockContext.Setup(m =&gt; m.Platforms).Returns(mockPlatformSet.Object);
          mockContext.Setup(m =&gt; m.Platforms.Add(It.IsAny&lt;Platform&gt;()));
          mockContext.Setup(m =&gt; m.Platforms.Add(It.IsAny&lt;Platform&gt;())).Callback(() =&gt; addCount++);
  
  
          unitOfWork = new UnitOfWork(mockContext.Object);
          platformRepo = new PlatformRepository(mockContext.Object);
  
          controller = new PlatformController(platformRepo, unitOfWork);
      }
  </code></pre>
  
  <p>Adding UnitOfWork code</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork 
  {
      private readonly DbContext _context;
      private bool _isDisposed = false;
  
      public UnitOfWork(DbContext context)
      {
          _context = context;
      }
  
      public void SaveChanges()
      {
          _context.SaveChanges();
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (!_isDisposed)
          {
              if (disposing)
              {
                  _context.Dispose();
              }
          }
          _isDisposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
  }
  </code></pre>
  
  <p><strong><em>Updated question</em></strong></p>
  
  <p>I am trying to Unit Test my <code>Create</code> method of my <code>PlatformController</code>. In this method, I call the <code>Add</code> function on the repostiory then the <code>SaveChanges</code> function of <code>UnitOfWork</code>. I want to verify that my <code>Platform</code> object gets added to the DbSet, but 'intercept' the call to <code>SaveChanges()</code> to that it does not write to db.</p>
  
  <p>How can I do this?</p>