8

I am new in mocking. I want to mock up my base repository which is depend on Entity Framework 6 DbContext But I fail. I searched in Google a lot but did not get any sufficient result. At last I got an example at testing with async queries and try to follow but it is worked for me.

Here is my code :

DbContext :

public class TimeSketchContext : DbContext
{
    public virtual DbSet<EmployeeSkill> EmployeeSkill { get; set; }
}

Base Repository :

public class BaseRepository<T> : IRepositoryBase<T> where T : class, IEntity, new()
{
    protected readonly DbContext InnerDbContext;
    protected DbSet<T> InnerDbSet;

    public BaseRepository(DbContext innerDbContext)
    {
        InnerDbContext = innerDbContext;
        InnerDbSet = InnerDbContext.Set<T>();
    }

    public virtual Task<T> FindAsync(long id)
    {
        return InnerDbSet.FirstOrDefaultAsync(x=>x.Id == id);
    }

}

Test :

    [Fact]
    public async Task DbTest()
    {
        var dummyData = GetEmployeeSkills();
        var mockSet = new Mock<DbSet<EmployeeSkill>>();

        mockSet.As<IDbAsyncEnumerable<EmployeeSkill>>()
            .Setup(x => x.GetAsyncEnumerator())
            .Returns(new TestDbAsyncEnumerator<EmployeeSkill>(dummyData.GetEnumerator()));

        mockSet.As<IQueryable<EmployeeSkill>>()
            .Setup(x => x.Provider)
            .Returns(new TestDbAsyncQueryProvider<EmployeeSkill>(dummyData.Provider));

        mockSet.As<IQueryable<EmployeeSkill>>().Setup(m => m.Expression).Returns(dummyData.Expression);
        mockSet.As<IQueryable<EmployeeSkill>>().Setup(m => m.ElementType).Returns(dummyData.ElementType);
        mockSet.As<IQueryable<EmployeeSkill>>().Setup(m => m.GetEnumerator()).Returns(dummyData.GetEnumerator());

        var mockContext = new Mock<TimeSketchContext>();
        mockContext.Setup(c => c.EmployeeSkill).Returns(mockSet.Object);

        var baseRepository = new BaseRepository<EmployeeSkill>(mockContext.Object);

        var data = await baseRepository.FindAsync(1);

        Assert.NotEqual(null, data);

    }

    private EmployeeSkill GetEmployeeSkill()
    {
        return new EmployeeSkill
        {
            SkillDescription = "SkillDescription",
            SkillName = "SkillName",
            Id = 1
        };
    }

    private IQueryable<EmployeeSkill> GetEmployeeSkills()
    {
        return new List<EmployeeSkill>
        {
            GetEmployeeSkill(),
            GetEmployeeSkill(),
            GetEmployeeSkill(),
        }.AsQueryable();
    }

Result is :

Assert.NotEqual() Failure

I think problem is

 public BaseRepository(DbContext innerDbContext)
 {
     InnerDbContext = innerDbContext;
     InnerDbSet = InnerDbContext.Set<T>();  <<<<<<<<<<<
 }

But don`t understand why and how to solve this.

I am using :

  • Visual Studio 2013 Ultimate
  • Moq
  • xUnit

Thank`s in advance.

Hasanuzzaman
  • 1,822
  • 5
  • 36
  • 54
  • 1
    For future readers: If you are injecting, say, a DbContext everywhere and you cannot abstract it to a repository/interface and you absolutely need to "mock" it, being non-virtual and all, you can use Microsoft Fakes Assemblies to accomplish this, as it does some fancy IL stuff to let you provide an alternative implementation to even a non-virtual method on an object. – Jesus is Lord Jun 28 '14 at 05:04
  • 1
    I strongly recommend checking out "Effort" (https://effort.codeplex.com/), an EF unit testing tool, which creates a real-enough EF database in-memory to test against. We went through a lot of headaches with mocking EF as well, but at the end of the day, testing EF mocks is an apples-to-oranges fiasco. (For one example, mock tests use L2O rather than L2E.) – RJB Sep 12 '14 at 16:57
  • its looks a nice tool, Will check Later. whatever thanks @RJB – Hasanuzzaman Sep 17 '14 at 05:58
  • For EF 6, I found the code in this article very useful: https://msdn.microsoft.com/en-us/library/dn314429.aspx I also ran into issues with mocking AsNoTracking(), and this brilliant answer solved the problem: http://stackoverflow.com/a/27087604/3507333 – madannes Apr 13 '16 at 16:38
  • That effort url comes up with a Google warning about dangerous programs ahead and advises not to continue. – Mordy May 17 '18 at 14:11

1 Answers1

11

You are right the problem is in your InnerDbContext.Set<T>(); statement.

In the current version of the EF (6.0.2) the DbContext.Set<T> method is not virtual so it cannot be mocked with Moq.

So you cannot easily make your test pass except by changing your design of the BaseRepository to not depend on the whole DbContext but on one DbSet<T>:

So something like:

public BaseRepository(DbSet<T> dbSet)
{
    InnerDbSet = dbSet;
}

Then you can pass directly in your mocked DbSet.

Or you can create a wrapper interface for DbContext:

public interface IDbContext
{
    DbSet<T> Set<T>() where T : class;
}

public class TimeSketchContext : DbContext, IDbContext
{
    public virtual DbSet<EmployeeSkill> EmployeeSkill { get; set; }
}

Then use IDbContext in your BaseRepository:

public class BaseRepository<T> : IRepositoryBase<T> where T : class, IEntity, new()
{
    protected readonly IDbContext InnerDbContext;
    protected DbSet<T> InnerDbSet;

    public BaseRepository(IDbContext innerDbContext)
    {
        InnerDbContext = innerDbContext;
        InnerDbSet = InnerDbContext.Set<T>();
    }

    public virtual Task<T> FindAsync(long id)
    {
        return InnerDbSet.FirstOrDefaultAsync(x => x.Id == id);
    }
}

And finally you just need to change two lines in your test to make it pass:

var mockContext = new Mock<IDbContext>();
mockContext.Setup(c => c.Set<EmployeeSkill>()).Returns(mockSet.Object);
Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
nemesv
  • 138,284
  • 16
  • 416
  • 359