[英]How to mock EF 6 Async methods using JustMock?
I am trying to Mock Entity Framework 6.0.2 Async methods using JustMock. 我正在尝试使用JustMock模拟实体框架6.0.2异步方法。 I am following testing with async queries but it is write using Moq I am trying to convert this into JustMock with help of Mock Multiple Interfaces but getting an exception : 我正在使用异步查询进行测试但是它是使用Moq编写的我试图在Mock Multiple Interfaces的帮助下将其转换为JustMock但是获得异常:
The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. 源IQueryable的提供程序未实现IDbAsyncQueryProvider。 Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations. 只有实现IDbAsyncQueryProvider的提供程序才能用于Entity Framework异步操作。 For more details see http://go.microsoft.com/fwlink/?LinkId=287068 . 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=287068 。
Here is my code : 这是我的代码:
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();
}
Test : 测试:
[Fact]
public async Task DbTest()
{
var data = await baseRepository.FindAsync(1);
Assert.NotEqual(null, data);
}
Repository : 存储库:
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;
}
Context : 背景:
public class TimeSketchContext : DbContext, IDbContext
{
public virtual DbSet<EmployeeSkill> EmployeeSkill { get; set; }
}
Because JustMock can mock non virtual methods when you are writing 因为JustMock可以在您编写时模拟非虚拟方法
var mockContext = Mock.Create<TimeSketchContext>();
mockContext.Arrange(x => x.Set<EmployeeSkill>()).Returns(mockSet);
it will mock the DbContext.Set<>
and not your IDbContext.Set<>
so you get the exception. 它会模拟DbContext.Set<>
而不是你的IDbContext.Set<>
所以你得到了异常。
There are at least 2 solution to this: 至少有两个解决方案:
Mock your IDbContext
interface 模拟你的IDbContext
接口
var mockContext = Mock.Create<IDbContext>();
Or change back your BaseRepository
to use a DbContext
instead of your interface: 或者更改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); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.