Mocking DbContext in Entity Framework 6.1
<p>I have found a number of examples that show (apparently) a clear working example of mocking DbContext with EF 6, however, none of them seem to work for me and I am not entirely sure why.</p>

<p>This is my unit test code that sets up the mock;</p>

<pre><code>var mockData = new List&lt;User&gt; { new User { Email = "my@email.com", Id = 1 } }.AsQueryable();

var mockSet = new Mock&lt;DbSet&lt;User&gt;&gt;();
    mockSet.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.Provider).Returns(mockData.Provider);
    mockSet.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.Expression).Returns(mockData.Expression);
    mockSet.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(mockData.ElementType);
    mockSet.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(mockData.GetEnumerator());

    var mockContext = new Mock&lt;MyDbContext&gt;();
    mockContext.Setup(c =&gt; c.Users).Returns(mockSet.Object);
</code></pre>

<p>and then the call to the service I am testing;</p>

<pre><code>var service = new UsersService(mockContext.Object);

var user = service.GetById(1);
</code></pre>

<p>This throws a NullReferenceException as the underlying DbSet is always null. The code does the following;</p>

<p>In BaseClass;</p>

<pre><code>public IEnumerable&lt;T&gt; GetAll()
{
    return _dbSet.AsEnumerable();
}
</code></pre>

<p>In subclass;</p>

<pre><code>  public User GetById(int id)
        {
            return GetAll().FirstOrDefault(x =&gt; x.Id == id);
        }
</code></pre>

<p>Please note that although there are other questions on SO that appear to be related, they do not cover EF 6.</p>

<p>For reference, this is an MSDN article that the same code with a modification to make it compile.</p>

<p><a href="https://msdn.microsoft.com/en-us/data/dn314429.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/data/dn314429.aspx</a></p>

<p>EDIT:</p>

<p>Reducing the complexity of the UserService (its uses generics/interfaces), the code is now simply;</p>

<pre><code>public User GetById(int id)
        {
            return _dbContext.Set&lt;User&gt;().FirstOrDefault(x =&gt; x.Id == id);
        }
</code></pre>

<p>If I change this further to;</p>

<pre><code>   var dbSet = _dbContext.Set&lt;User&gt;();
        return dbSet.FirstOrDefault(x =&gt; x.Id == id);
</code></pre>

<p>I can clearly see that dbSet is null.</p>

<p>Edit 2</p>

<p>As per the suggestion from wablab, it appears that mock .Set resolved the problem.</p>

<p>Credit also to Vladyslav Kushnir for the Generic method for DbSet.</p>

<p>Working code for this for anyone that might need it;</p>

<pre><code> private static Mock&lt;DbSet&lt;T&gt;&gt; GetDbSetMock&lt;T&gt;(IEnumerable&lt;T&gt; items = null) where T : class
        {
            if (items == null)
            {
                items = new T[0];
            }

            var dbSetMock = new Mock&lt;DbSet&lt;T&gt;&gt;();
            var q = dbSetMock.As&lt;IQueryable&lt;T&gt;&gt;();

            q.Setup(x =&gt; x.GetEnumerator()).Returns(items.GetEnumerator);

            return dbSetMock;
        }



var mockContext = new Mock&lt;Model1&gt;();

var users = new List&lt;User&gt; { new User { Email = "my@email.com", Id = 1 } };

mockContext.Setup(x =&gt; x.Set&lt;User&gt;()).Returns(GetDbSetMock(users).Object);

var service = new UsersService(mockContext.Object);

var user = service.GetById(1);
</code></pre>