gpt4 book ai didi

c# - 如何使用 JustMock 模拟 EF 6 异步方法?

转载 作者:太空狗 更新时间:2023-10-29 23:39:08 27 4
gpt4 key购买 nike

我正在尝试使用 JustMock 模拟 Entity Framework 6.0.2 异步方法。我正在关注 testing with async queries但它是使用最小起订量编写的我试图在Mock Multiple Interfaces的帮助下将其转换为JustMock但出现异常:

The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.

这是我的代码:

var dummyData = GetEmployeeSkills();
var mockSet = Mock.Create<DbSet<EmployeeSkill>>();
(mockSet as IDbAsyncEnumerable<EmployeeSkill>).Arrange(x => x.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<EmployeeSkill>(dummyData.GetEnumerator()));

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

(mockSet as IQueryable<EmployeeSkill>).Arrange(x => x.Expression).Returns(dummyData.Expression);
(mockSet as IQueryable<EmployeeSkill>).Arrange(x => x.ElementType).Returns(dummyData.ElementType);
(mockSet as IQueryable<EmployeeSkill>).Arrange(x => x.GetEnumerator()).Returns(dummyData.GetEnumerator());

var mockContext = Mock.Create<TimeSketchContext>();
mockContext.Arrange(x => x.Set<EmployeeSkill>()).Returns(mockSet);

baseRepository = new BaseRepository<EmployeeSkill>(mockContext);

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

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

测试:

[Fact]
public async Task DbTest()
{
var data = await baseRepository.FindAsync(1);
Assert.NotEqual(null, data);
}

存储库:

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

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

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

接口(interface):

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

上下文:

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

最佳答案

因为JustMock可以在你写的时候mock非虚方法

var mockContext = Mock.Create<TimeSketchContext>();
mockContext.Arrange(x => x.Set<EmployeeSkill>()).Returns(mockSet);

它将模拟 DbContext.Set<>而不是你的 IDbContext.Set<>所以你得到了异常(exception)。

至少有两种解决方案:

  • 模拟你的 IDbContext界面

    var mockContext = Mock.Create<IDbContext>();
  • 或者改回你的BaseRepository使用 DbContext而不是你的界面:

    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);
    }
    }

关于c# - 如何使用 JustMock 模拟 EF 6 异步方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com