title: Moq testing Entity Framework
body: <p>I am newbie to both <code>Entity Framework</code> and <code>Moq testing</code>.</p>
  <p>Below is my <code>EF code</code>:</p>
  <pre><code>public class Repository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
  {
  
      protected readonly DbContext Context;
      public Repository(DbContext context)
      {
          Context = context;
      }
  
      public TEntity Get(int id)
      {
        
          return Context.Set&lt;TEntity&gt;().Find(id);
      }
  
      public IEnumerable&lt;TEntity&gt; GetAll()
      {
         
          return Context.Set&lt;TEntity&gt;().ToList();
      }
  
      public void Add(TEntity entity)
      {
          Context.Set&lt;TEntity&gt;().Add(entity);
      }
  
      public void Remove(TEntity entity)
      {
          Context.Set&lt;TEntity&gt;().Remove(entity);
      }
  
  }
  
  
  public interface IRepository&lt;TEntity&gt; where TEntity : class
  {
  
      TEntity Get(int id);
      IEnumerable&lt;TEntity&gt; GetAll();
  
  
      void Add(TEntity entity);
  
      void Remove(TEntity entity);
  
  }
  
  
  public interface IUnitOfWork : IDisposable
  {
  
      int Complete();
  }
  
  
  public class UnitOfWork : IUnitOfWork
  {
  
  
      private readonly EF_SalesOrdersDBContext _context;
  
      public CustomersRepository customersRepository { get; set; }
  
      public UnitOfWork(EF_SalesOrdersDBContext context)
      {
          _context = context;
          customersRepository = new CustomersRepository(_context);
      }
  
  
  
      public int Complete()
      {
          return _context.SaveChanges();
      }
  
      public void Dispose()
      {
          _context.Dispose();
      }
  }
  
  public class CustomersRepository : Repository&lt;Customer&gt;
  {
      public CustomersRepository(EF_SalesOrdersDBContext context)
          :base(context)
      {
              
      }
  
  
  
  }
  </code></pre>
  <p>Code below in <code>program.cs</code> which works.</p>
  <pre><code>   UnitOfWork unitOfWork = new UnitOfWork(new EF_SalesOrdersDBContext());
          unitOfWork.customersRepository.Add(new Customer { CompanyName = &quot;test2&quot;, FirstName = &quot;John&quot;, LastName = &quot;Barnes&quot;, AddressLine1 = &quot;11 lfc&quot;, City = &quot;Liverpool&quot;, County = &quot;LFC&quot;, Phone = &quot;444&quot; });         
          unitOfWork.Complete();
  </code></pre>
  <p>I would like test using <code>Moq</code>.</p>
  <p>This is what I have tried:</p>
  <pre><code> [TestClass]
  public class UnitTest1
  {
      [TestMethod]
      public void TestMethod1()
      {
    
          var mockSet = new Mock&lt;DbSet&lt;Customer&gt;&gt;();
  
          var mockContext = new Mock&lt;EF_SalesOrdersDBContext&gt;();
          mockContext.Setup(m =&gt; m.Customers).Returns(mockSet.Object);
  
  
          var service = new CustomersRepository(mockContext.Object);
  
     
  
          service.Add(new Customer { CompanyName = &quot;test2&quot;, FirstName = &quot;John&quot;, LastName = &quot;Barnes&quot;, AddressLine1 = &quot;11 lfc&quot;, City = &quot;Liverpool&quot;, County = &quot;LFC&quot;, Phone = &quot;444&quot; });
  
          mockSet.Verify(m =&gt; m.Add(It.IsAny&lt;Customer&gt;()), Times.Once());
          mockContext.Verify(m =&gt; m.SaveChanges(), Times.Once());
  
  
      }
  
  }
  </code></pre>
  <p>I get the error:</p>
  <blockquote>
  <p>Message:
  Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception:
  System.NullReferenceException: Object reference not set to an instance of an object.   Stack Trace:
  Repository`1.Add(TEntity entity) line 34
  UnitTest1.TestMethod1() line 26</p>
  </blockquote>