How to mock Entity Framework 6 Async methods?

前端 未结 1 1050
北恋
北恋 2020-12-31 21:34

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 sufficie

相关标签:
1条回答
  • 2020-12-31 21:39

    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);
    
    0 讨论(0)
提交回复
热议问题