title: Entity Framework 4, MOQ,
body: <p>I am using EF4, Microsoft.Entity.CTP, and the latest MOQ. I am trying to create a generic repository class and moq the DBContext using MOQ. Whenever I run my moq test I get "object reference not set to an instance of an object" on this.context.Set().Add(entity); and I don't understand why. The code runs ok without a moq.</p>
  
  <pre><code>public class GenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; where TEntity : class
  {
      private IContext context;
  
      public GenericRepository(IContext context)
      {
          this.context = context;
      }
  
      public IList&lt;TEntity&gt; List
      {
          get { return context.Set&lt;TEntity&gt;().ToList(); }
      }
  
      public void Create(TEntity entity)
      {
          this.context.Set&lt;TEntity&gt;().Add(entity);
          this.context.SaveChanges();
      }
  }
  
  var mock = new Mock&lt;IContext&gt;();
  GenericRepository&lt;Product&gt; producRepository = new GenericRepository&lt;Product&gt;(mock.Object);
  
  mock.Setup(x =&gt; x.Product.Add(productType));
  mock.Setup(x =&gt; x.SaveChanges());
  
  productRepository.Create(product);
  mock.VerifyAll();
  </code></pre>