Unit test an Entity Framework generic repository using Moq
<p><strong>Question/Problem</strong></p>

<p>I am not able to get a passing test because the <strong>Generic Repository</strong> class <code>this.dbSet = context.Set&lt;T&gt;();</code> is always <code>null</code>.  As you can see in the code below, I have mocked up the <code>DbSet</code> and the context. I have also setup the mocked context to return mocked <code>DbSet</code>. The <code>EnityRepository</code> constructor takes the mocked context as expected, but <code>this.dbSet = context.Set&lt;T&gt;();</code> isn't picking up my mocked <code>DbSet</code>. I'm not sure what I did wrong. Am I not mocking this the right way?</p>

<p><strong>Structure:</strong></p>

<ul>
<li><strong>DAL</strong> - entity framework, generic repository, unit of work </li>
<li><strong>BLL</strong> -
service, automapper (mapping entity generated classes/objects to
business objects) </li>
<li><strong>Interface</strong> - <code>IService</code> </li>
<li><strong>Model</strong> - business objects </li>
<li><strong>Web</strong> - ASP.NET MVC  </li>
<li><strong>Test</strong> - unit test</li>
</ul>

<p><strong>Generic Repository</strong></p>

<pre class="lang-cs prettyprint-override"><code>public class EntityRepository&lt;T&gt; : IEntityRepository&lt;T&gt; where T : class
{
    internal MyDB_Entities context;
    internal DbSet&lt;T&gt; dbSet;

    public EntityRepository(MyDB_Entities context)
    {
        this.context = context;
        this.dbSet = context.Set&lt;T&gt;();
    }

    public virtual T GetByID(object id)
    {
        return dbSet.Find(id);
    }

    // more code
}
</code></pre>

<p><strong>Interface for Generic Repository</strong></p>

<pre class="lang-cs prettyprint-override"><code>public interface IEntityRepository&lt;T&gt; where T : class
{ 
    IEnumerable&lt;T&gt; Get(Expression&lt;Func&lt;T, bool&gt;&gt; filter = null, Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderBy = null, string includeProperties = "");
    T GetByID(object id);
    // more code
}
</code></pre>

<p><strong>Unit of Work</strong></p>

<pre class="lang-cs prettyprint-override"><code>public class UnitOfWork : IUnitOfWork, IDisposable
{
    MyDB_Entities _context;
    public IEntityRepository&lt;Customer&gt; customerRepository { get; set; }
    public IEntityRepository&lt;Product&gt; productRepository { get; set; }

    public UnitOfWork(MyDB_Entities context)
    {
        _context = context;
        customerRepository = new EntityRepository&lt;Customer&gt;(_context);
        productRepository = new EntityRepository&lt;Product&gt;(_context); 
    }

    public void Save()
    {
        _context.SaveChanges();
    }
    // more code
}
</code></pre>

<p><strong>Interface for Unit Of Work</strong></p>

<pre class="lang-cs prettyprint-override"><code>public interface IUnitOfWork
{
    IEntityRepository&lt;Customer&gt; customerRepository { get; set; }
    IEntityRepository&lt;Product&gt; productRepository { get; set; }
    void Dispose();
    void Save();
}
</code></pre>

<p><strong>Service</strong></p>

<pre class="lang-cs prettyprint-override"><code>public class SomeService : ISomeService 
{
    readonly IUnitOfWork _unitOfWork;
    public SomeService (IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    // DoSomethingMethod
}
</code></pre>

<p><strong>Interface for Service</strong></p>

<pre class="lang-cs prettyprint-override"><code>public interface ISomeService
{
    // IDoSomethingMethod 
}
</code></pre>

<p><strong>Extension</strong></p>

<pre class="lang-cs prettyprint-override"><code>public static class MockDBSetExtension
{
    public static void SetSource&lt;T&gt;(this Mock&lt;DbSet&lt;T&gt;&gt; mockSet, IList&lt;T&gt; source) where T : class
    {
        var data = source.AsQueryable();
        mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Provider).Returns(data.Provider);
        mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Expression).Returns(data.Expression);
        mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(data.ElementType);
        mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(data.GetEnumerator());
    }
}
</code></pre>

<p><strong>Test Class</strong></p>

<pre class="lang-cs prettyprint-override"><code>[TestClass]
public class My_Test
{
    Mock&lt;DbSet&lt;Product&gt;&gt; _mockProductDBSet;
    Mock&lt;MyDB_Entities&gt; mockContext;

    [TestInitialize]
    public void TestInitialize()
    {
        _mockProductDBSet = new Mock&lt;DbSet&lt;Product&gt;&gt;();
        mockContext = new Mock&lt;MyDB_Entities&gt;();
        mockContext.Setup(s =&gt; s.Products).Returns(_mockProductDBSet.Object);
    }

    [TestMethod]
    public void TestMocking()
    {
       var prod = new Product() { ProductName= "AAA", ProductID = 1 };
        _mockProductDBSet.SetSource(new List&lt;Product&gt; { prod });
       // more code here (new up the service, then test the service method, etc)
    }
}
</code></pre>